Skip to content

Commit 55ec980

Browse files
committed
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.
1 parent 07af2f1 commit 55ec980

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-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
@@ -20,10 +20,6 @@ const {
2020
globalThis,
2121
} = primordials;
2222

23-
const {
24-
Intl,
25-
} = globalThis;
26-
2723
const {
2824
getOptionValue,
2925
refreshOptions,
@@ -333,7 +329,6 @@ function setupNavigator() {
333329

334330
// https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
335331
exposeLazyInterfaces(globalThis, 'internal/navigator', ['Navigator']);
336-
internalBinding('config').language = Intl?.Collator().resolvedOptions().locale || 'en-US';
337332
defineReplaceableLazyAttribute(globalThis, 'internal/navigator', ['navigator'], false);
338333
}
339334

src/node_config.cc

+22-2
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,32 @@
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;
18+
using v8::String;
1619
using v8::Value;
1720

21+
void GetDefaultLocale(const FunctionCallbackInfo<Value>& args) {
22+
Isolate* isolate = args.GetIsolate();
23+
Local<Context> context = isolate->GetCurrentContext();
24+
std::string locale = isolate->GetDefaultLocale();
25+
Local<Value> result;
26+
if (ToV8Value(context, locale).ToLocal(&result)) {
27+
args.GetReturnValue().Set(result);
28+
}
29+
}
30+
1831
// The config binding is used to provide an internal view of compile time
1932
// config options that are required internally by lib/*.js code. This is an
2033
// alternative to dropping additional properties onto the process object as
@@ -23,7 +36,7 @@ using v8::Value;
2336
// Command line arguments are already accessible in the JS land via
2437
// require('internal/options').getOptionValue('--some-option'). Do not add them
2538
// here.
26-
static void Initialize(Local<Object> target,
39+
static void InitConfig(Local<Object> target,
2740
Local<Value> unused,
2841
Local<Context> context,
2942
void* priv) {
@@ -76,8 +89,15 @@ static void Initialize(Local<Object> target,
7689
#endif // NODE_NO_BROWSER_GLOBALS
7790

7891
READONLY_PROPERTY(target, "bits", Number::New(isolate, 8 * sizeof(intptr_t)));
92+
93+
SetMethodNoSideEffect(context, target, "getDefaultLocale", GetDefaultLocale);
7994
} // InitConfig
8095

96+
void RegisterConfigExternalReferences(ExternalReferenceRegistry* registry) {
97+
registry->Register(GetDefaultLocale);
98+
}
99+
81100
} // namespace node
82101

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

src/node_external_reference.h

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class ExternalReferenceRegistry {
131131
V(buffer) \
132132
V(builtins) \
133133
V(cares_wrap) \
134+
V(config) \
134135
V(contextify) \
135136
V(credentials) \
136137
V(encoding_binding) \

0 commit comments

Comments
 (0)