Skip to content

Commit 0f146aa

Browse files
jasnelladuh95
authored andcommitted
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. PR-URL: #53649 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent e6a1eeb commit 0f146aa

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

lib/internal/navigator.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ function getNavigatorPlatform(process) {
7878
class Navigator {
7979
// Private properties are used to avoid brand validations.
8080
#availableParallelism;
81-
#userAgent = `Node.js/${StringPrototypeSlice(nodeVersion, 1, StringPrototypeIndexOf(nodeVersion, '.'))}`;
82-
#platform = getNavigatorPlatform(process);
83-
#language = Intl?.Collator().resolvedOptions().locale || 'en-US';
84-
#languages = ObjectFreeze([this.#language]);
81+
#userAgent;
82+
#platform;
83+
#language;
84+
#languages;
8585

8686
constructor() {
8787
if (arguments[0] === kInitialize) {
@@ -102,27 +102,31 @@ class Navigator {
102102
* @return {string}
103103
*/
104104
get language() {
105+
this.#language ??= Intl?.Collator().resolvedOptions().locale || 'en-US';
105106
return this.#language;
106107
}
107108

108109
/**
109110
* @return {Array<string>}
110111
*/
111112
get languages() {
113+
this.#languages ??= ObjectFreeze([this.language]);
112114
return this.#languages;
113115
}
114116

115117
/**
116118
* @return {string}
117119
*/
118120
get userAgent() {
121+
this.#userAgent ??= `Node.js/${StringPrototypeSlice(nodeVersion, 1, StringPrototypeIndexOf(nodeVersion, '.'))}`;
119122
return this.#userAgent;
120123
}
121124

122125
/**
123126
* @return {string}
124127
*/
125128
get platform() {
129+
this.#platform ??= getNavigatorPlatform(process);
126130
return this.#platform;
127131
}
128132
}

0 commit comments

Comments
 (0)