-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib: add navigator.hardwareConcurrency
#47769
Changes from all commits
1831249
3ebb71b
10f375e
db57036
40cf5ff
3eccea4
1bca99b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -583,6 +583,41 @@ The `MessagePort` class. See [`MessagePort`][] for more details. | |
|
||
This variable may appear to be global but is not. See [`module`][]. | ||
|
||
## `Navigator` | ||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
> Stability: 1 - Experimental | ||
|
||
An implementation of the [Navigator API][]. | ||
|
||
## `navigator` | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
> Stability: 1 - Experimental | ||
|
||
An implementation of [`window.navigator`][]. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On a closer look, I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not support both? I suggest adding WorkerNavigator in a different pull request. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would find it confusing to expose both of them: every Web interface has an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. How can I limit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC, we didn't distinguish I'd agree that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't really matter now, as we implement the But I think we should link to |
||
|
||
### `navigator.hardwareConcurrency` | ||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
* {number} | ||
|
||
The `navigator.hardwareConcurrency` read-only property returns the number of | ||
logical processors available to the current Node.js instance. | ||
|
||
```js | ||
console.log(`This process is running on ${navigator.hardwareConcurrency}`); | ||
``` | ||
|
||
## `PerformanceEntry` | ||
|
||
<!-- YAML | ||
|
@@ -998,6 +1033,7 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][]. | |
|
||
[CommonJS module]: modules.md | ||
[ECMAScript module]: esm.md | ||
[Navigator API]: https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object | ||
[Web Crypto API]: webcrypto.md | ||
[`--no-experimental-fetch`]: cli.md#--no-experimental-fetch | ||
[`--no-experimental-global-customevent`]: cli.md#--no-experimental-global-customevent | ||
|
@@ -1057,6 +1093,7 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][]. | |
[`setInterval`]: timers.md#setintervalcallback-delay-args | ||
[`setTimeout`]: timers.md#settimeoutcallback-delay-args | ||
[`structuredClone`]: https://developer.mozilla.org/en-US/docs/Web/API/structuredClone | ||
[`window.navigator`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/navigator | ||
[buffer section]: buffer.md | ||
[built-in objects]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects | ||
[module system documentation]: modules.md | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
const { | ||
ObjectDefineProperties, | ||
Symbol, | ||
} = primordials; | ||
|
||
const { | ||
ERR_ILLEGAL_CONSTRUCTOR, | ||
} = require('internal/errors').codes; | ||
|
||
const { | ||
kEnumerableProperty, | ||
} = require('internal/util'); | ||
|
||
const { | ||
getAvailableParallelism, | ||
} = internalBinding('os'); | ||
|
||
const kInitialize = Symbol('kInitialize'); | ||
|
||
class Navigator { | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Private properties are used to avoid brand validations. | ||
#availableParallelism; | ||
|
||
constructor() { | ||
if (arguments[0] === kInitialize) { | ||
return; | ||
} | ||
throw ERR_ILLEGAL_CONSTRUCTOR(); | ||
} | ||
|
||
/** | ||
* @return {number} | ||
*/ | ||
get hardwareConcurrency() { | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.#availableParallelism ??= getAvailableParallelism(); | ||
return this.#availableParallelism; | ||
} | ||
} | ||
|
||
ObjectDefineProperties(Navigator.prototype, { | ||
hardwareConcurrency: kEnumerableProperty, | ||
}); | ||
|
||
module.exports = { | ||
navigator: new Navigator(kInitialize), | ||
Navigator, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
const is = { | ||
number: (value, key) => { | ||
assert(!Number.isNaN(value), `${key} should not be NaN`); | ||
assert.strictEqual(typeof value, 'number'); | ||
}, | ||
}; | ||
|
||
is.number(+navigator.hardwareConcurrency, 'hardwareConcurrency'); | ||
is.number(navigator.hardwareConcurrency, 'hardwareConcurrency'); | ||
assert.ok(navigator.hardwareConcurrency > 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we mention this is not a full implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m -0 on this, but since you didn’t block the PR, I recommend merging this PR, if you’re ok with it @MoLow