-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: add
navigator.hardwareConcurrency
Co-authored-by: Mestery <[email protected]> Co-authored-by: Voltrex <[email protected]> PR-URL: #47769 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Moshe Atlow <[email protected]>
- Loading branch information
1 parent
74c2d9c
commit b40f0c3
Showing
8 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
// Private properties are used to avoid brand validations. | ||
#availableParallelism; | ||
|
||
constructor() { | ||
if (arguments[0] === kInitialize) { | ||
return; | ||
} | ||
throw ERR_ILLEGAL_CONSTRUCTOR(); | ||
} | ||
|
||
/** | ||
* @return {number} | ||
*/ | ||
get hardwareConcurrency() { | ||
this.#availableParallelism ??= getAvailableParallelism(); | ||
return this.#availableParallelism; | ||
} | ||
} | ||
|
||
ObjectDefineProperties(Navigator.prototype, { | ||
hardwareConcurrency: kEnumerableProperty, | ||
}); | ||
|
||
module.exports = { | ||
navigator: new Navigator(kInitialize), | ||
Navigator, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |