Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
joyeecheung committed Aug 19, 2024
commit 4857714108669d35d713d3c062a6146beecc888f
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';
Comment thread
joyeecheung marked this conversation as resolved.
Resolved
}

/**
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 @@ -20,10 +20,6 @@ const {
globalThis,
} = primordials;

const {
Intl,
} = globalThis;

const {
getOptionValue,
refreshOptions,
Expand Down Expand Up @@ -333,7 +329,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
24 changes: 22 additions & 2 deletions src/node_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@
#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::String;
Comment thread
joyeecheung marked this conversation as resolved.
Resolved
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 +36,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 +89,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