1. physical-cpu-count
Provides the number of physical cores
physical-cpu-count
Package: physical-cpu-count
Last modified: Thu, 23 Jun 2022 22:17:08 GMT
Version: 2.0.0
License: ISC
Downloads: 1,472,588

Install

npm install physical-cpu-count
yarn add physical-cpu-count

physical-cpu-count

Returns the number of physical CPU cores.

Example

 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:

Dependencies

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code