Skip to content

Commit

Permalink
lib: make navigator properties lazy
Browse files Browse the repository at this point in the history
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.

PR-URL: #53649
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
jasnell authored and aduh95 committed Jul 12, 2024
1 parent a83bae6 commit 1f6fcd6
Showing 1 changed file with 8 additions and 4 deletions.
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]);
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

0 comments on commit 1f6fcd6

Please sign in to comment.