Skip to content

Commit

Permalink
src: use v8::Isolate::GetDefaultLocale() to compute navigator.language
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
joyeecheung authored and RafaelGSS committed Aug 30, 2024
1 parent b00c087 commit f8cbbc6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
6 changes: 4 additions & 2 deletions lib/internal/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const {
} = require('internal/process/per_thread');

const {
language,
getDefaultLocale,
} = internalBinding('config');

/**
Expand Down Expand Up @@ -104,7 +104,9 @@ class Navigator {
* @return {string}
*/
get language() {
return language;
// The default locale might be changed dynamically, so always invoke the
// binding.
return getDefaultLocale() || 'en-US';
}

/**
Expand Down
5 changes: 0 additions & 5 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ const {
globalThis,
} = primordials;

const {
Intl,
} = globalThis;

const {
getOptionValue,
refreshOptions,
Expand Down Expand Up @@ -347,7 +343,6 @@ function setupNavigator() {

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

Expand Down
23 changes: 21 additions & 2 deletions src/node_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@
#include "memory_tracker.h"
#include "node.h"
#include "node_builtins.h"
#include "node_external_reference.h"
#include "node_i18n.h"
#include "node_options.h"
#include "util-inl.h"

namespace node {

using v8::Context;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::Value;

void GetDefaultLocale(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
std::string locale = isolate->GetDefaultLocale();
Local<Value> result;
if (ToV8Value(context, locale).ToLocal(&result)) {
args.GetReturnValue().Set(result);
}
}

// The config binding is used to provide an internal view of compile time
// config options that are required internally by lib/*.js code. This is an
// alternative to dropping additional properties onto the process object as
Expand All @@ -23,7 +35,7 @@ using v8::Value;
// Command line arguments are already accessible in the JS land via
// require('internal/options').getOptionValue('--some-option'). Do not add them
// here.
static void Initialize(Local<Object> target,
static void InitConfig(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Expand Down Expand Up @@ -76,8 +88,15 @@ static void Initialize(Local<Object> target,
#endif // NODE_NO_BROWSER_GLOBALS

READONLY_PROPERTY(target, "bits", Number::New(isolate, 8 * sizeof(intptr_t)));

SetMethodNoSideEffect(context, target, "getDefaultLocale", GetDefaultLocale);
} // InitConfig

void RegisterConfigExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(GetDefaultLocale);
}

} // namespace node

NODE_BINDING_CONTEXT_AWARE_INTERNAL(config, node::Initialize)
NODE_BINDING_CONTEXT_AWARE_INTERNAL(config, node::InitConfig)
NODE_BINDING_EXTERNAL_REFERENCE(config, node::RegisterConfigExternalReferences)
1 change: 1 addition & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class ExternalReferenceRegistry {
V(buffer) \
V(builtins) \
V(cares_wrap) \
V(config) \
V(contextify) \
V(credentials) \
V(encoding_binding) \
Expand Down

0 comments on commit f8cbbc6

Please sign in to comment.