Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including physical-cpu-count with all npm packages installed. Try it out:

var physicalCpuCount = require("physical-cpu-count")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

physical-cpu-count v2.0.0

Provides the number of physical cores


Returns the number of physical CPU cores.


const physicalCpuCount = require('physical-cpu-count')
// 4

const logicalCpuCount = require('os').cpus().length
// 8

Use Case

Working with clusters of Node.js processes it is common to see code using os.cpus().length as the number of child workers to fork. For some workloads this can negatively impact performance on CPUs that use simultaneous multithreading (SMT). Latency is doubled because two processes share the same physical CPU core to get their work done. Additionally there is memory spent for each running worker, as well as time to spawn their processes. It is better to fork no more child processes than there are physical cores.

Known Limitations

Implemented for Linux, macOS, and Windows.

Other platforms use a naive approach that only looks at Intel CPUs, and assumes every Intel CPU supports, and has enabled, Hyper-Threading with two threads per physical core. These assumptions are not always correct.

Power management might also make CPU cores unavailable.

See also:


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free