Use-Cases of this Tutorial
- Know how to get the number of CPU cores in the device.
- Know about the navigator.hardwareConcurrency property.
The number of hardware cores in a processor represents the number of threads that the processor can execute in parallel. A processor having only a single core can run one thread and so perform only one operation at one time. A processor having two cores can run two threads, so perform two operations in parallel. An so on.
Modern processors are capable of executing more than one thread in a single physical core through hyperthreading. A single physical core can even run multiple threads on it. So a single physical core may become multiple logical cores (2 physical cores may in total become 4 logical cores). Each logical core can run one thread, and all logical cores can run in parallel.
// gets the "available" logical processors count var logicalProcessorCount = navigator.hardwareConcurrency;
It is important not to treat this count as the actual number of logical cores present in the device. Depending upon various factors such as the number of cores used up, or the number of cores actually available for work, browser may return a lower number of logical cores.
So navigator.hardwareConcurency can be useful in knowing the limit of Web Workers that can be executed by the application at a single time.