Skip to content
Merged
Changes from all commits
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
lib: make navigator properties lazy
Noticed in some benchmarking/profiling that the Navigator object
constructor was rather expensive and slow due to initialization
of properties during construction. It makes more sense for these
to be lazily initialized on first access.
  • Loading branch information
jasnell committed Jun 30, 2024
commit 875952b226f893c42eaa91e7be2f45e9a807a517
12 changes: 8 additions & 4 deletions lib/internal/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ function getNavigatorPlatform(process) {
class Navigator {
// Private properties are used to avoid brand validations.
#availableParallelism;
#userAgent = `Node.js/${StringPrototypeSlice(nodeVersion, 1, StringPrototypeIndexOf(nodeVersion, '.'))}`;
#platform = getNavigatorPlatform(process);
#language = Intl?.Collator().resolvedOptions().locale || 'en-US';
#languages = ObjectFreeze([this.#language]);
#userAgent;
#platform;
#language;
#languages;

constructor() {
if (arguments[0] === kInitialize) {
Expand All @@ -102,27 +102,31 @@ class Navigator {
* @return {string}
*/
get language() {
this.#language ??= Intl?.Collator().resolvedOptions().locale || 'en-US';
return this.#language;
}

/**
* @return {Array<string>}
*/
get languages() {
this.#languages ??= ObjectFreeze([this.language]);
Comment thread
jasnell marked this conversation as resolved.
return this.#languages;
}

/**
* @return {string}
*/
get userAgent() {
this.#userAgent ??= `Node.js/${StringPrototypeSlice(nodeVersion, 1, StringPrototypeIndexOf(nodeVersion, '.'))}`;
return this.#userAgent;
}

/**
* @return {string}
*/
get platform() {
this.#platform ??= getNavigatorPlatform(process);
return this.#platform;
}
}
Expand Down