Skip to content

Commit f8cbbc6

Browse files
joyeecheungRafaelGSS
authored andcommittedAug 30, 2024
src: use v8::Isolate::GetDefaultLocale() to compute navigator.language
Using the Intl API to get the default locale slows down the startup significantly. This patch uses a new v8 API to get the default locale directly. PR-URL: #54279 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b00c087 commit f8cbbc6

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed
 

‎lib/internal/navigator.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const {
3030
} = require('internal/process/per_thread');
3131

3232
const {
33-
language,
33+
getDefaultLocale,
3434
} = internalBinding('config');
3535

3636
/**
@@ -104,7 +104,9 @@ class Navigator {
104104
* @return {string}
105105
*/
106106
get language() {
107-
return language;
107+
// The default locale might be changed dynamically, so always invoke the
108+
// binding.
109+
return getDefaultLocale() || 'en-US';
108110
}
109111

110112
/**

‎lib/internal/process/pre_execution.js

-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ const {
2121
globalThis,
2222
} = primordials;
2323

24-
const {
25-
Intl,
26-
} = globalThis;
27-
2824
const {
2925
getOptionValue,
3026
refreshOptions,
@@ -347,7 +343,6 @@ function setupNavigator() {
347343

348344
// https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
349345
exposeLazyInterfaces(globalThis, 'internal/navigator', ['Navigator']);
350-
internalBinding('config').language = Intl?.Collator().resolvedOptions().locale || 'en-US';
351346
defineReplaceableLazyAttribute(globalThis, 'internal/navigator', ['navigator'], false);
352347
}
353348

‎src/node_config.cc

+21-2
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,31 @@
22
#include "memory_tracker.h"
33
#include "node.h"
44
#include "node_builtins.h"
5+
#include "node_external_reference.h"
56
#include "node_i18n.h"
67
#include "node_options.h"
78
#include "util-inl.h"
89

910
namespace node {
1011

1112
using v8::Context;
13+
using v8::FunctionCallbackInfo;
1214
using v8::Isolate;
1315
using v8::Local;
1416
using v8::Number;
1517
using v8::Object;
1618
using v8::Value;
1719

20+
void GetDefaultLocale(const FunctionCallbackInfo<Value>& args) {
21+
Isolate* isolate = args.GetIsolate();
22+
Local<Context> context = isolate->GetCurrentContext();
23+
std::string locale = isolate->GetDefaultLocale();
24+
Local<Value> result;
25+
if (ToV8Value(context, locale).ToLocal(&result)) {
26+
args.GetReturnValue().Set(result);
27+
}
28+
}
29+
1830
// The config binding is used to provide an internal view of compile time
1931
// config options that are required internally by lib/*.js code. This is an
2032
// alternative to dropping additional properties onto the process object as
@@ -23,7 +35,7 @@ using v8::Value;
2335
// Command line arguments are already accessible in the JS land via
2436
// require('internal/options').getOptionValue('--some-option'). Do not add them
2537
// here.
26-
static void Initialize(Local<Object> target,
38+
static void InitConfig(Local<Object> target,
2739
Local<Value> unused,
2840
Local<Context> context,
2941
void* priv) {
@@ -76,8 +88,15 @@ static void Initialize(Local<Object> target,
7688
#endif // NODE_NO_BROWSER_GLOBALS
7789

7890
READONLY_PROPERTY(target, "bits", Number::New(isolate, 8 * sizeof(intptr_t)));
91+
92+
SetMethodNoSideEffect(context, target, "getDefaultLocale", GetDefaultLocale);
7993
} // InitConfig
8094

95+
void RegisterConfigExternalReferences(ExternalReferenceRegistry* registry) {
96+
registry->Register(GetDefaultLocale);
97+
}
98+
8199
} // namespace node
82100

83-
NODE_BINDING_CONTEXT_AWARE_INTERNAL(config, node::Initialize)
101+
NODE_BINDING_CONTEXT_AWARE_INTERNAL(config, node::InitConfig)
102+
NODE_BINDING_EXTERNAL_REFERENCE(config, node::RegisterConfigExternalReferences)

‎src/node_external_reference.h

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class ExternalReferenceRegistry {
139139
V(buffer) \
140140
V(builtins) \
141141
V(cares_wrap) \
142+
V(config) \
142143
V(contextify) \
143144
V(credentials) \
144145
V(encoding_binding) \

0 commit comments

Comments
 (0)
Please sign in to comment.