Navigator: hardwareConcurrency property
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2022.
Thenavigator.hardwareConcurrency read-only propertyreturns the number of logical processors available to run threads on the user'scomputer.
In this article
Value
A number between 1 and the number of logical processors potentially available to the user agent.
Modern computers have multiple physical processor cores in their CPU (two or four coresis typical), but each physical core is also usually able to run more than one thread ata time using advanced scheduling techniques. So a four-core CPU may offer eightlogical processor cores, for example. The number of logical processorcores can be used to measure the number of threads which can effectively be run at oncewithout them having to context switch.
The browser may, however, choose to report a lower number of logical cores in order torepresent more accurately the number ofWorkers that can run at once, sodon't treat this as an absolute measurement of the number of cores in the user's system.
Examples
In this example, oneWorker is created for each logical processorreported by the browser and a record is created which includes a reference to the newworker as well as a Boolean value indicating whether or not we're using that worker yet;these objects are, in turn, stored into an array for later use. This creates a pool ofworkers we can use to process requests later.
let workerList = [];for (let i = 0; i < window.navigator.hardwareConcurrency; i++) { let newWorker = { worker: new Worker("cpu-worker.js"), inUse: false, }; workerList.push(newWorker);}Specifications
| Specification |
|---|
| HTML> # dom-navigator-hardwareconcurrency-dev> |