os.cpus()
Returns an array of objects describing each logical CPU core.
Syntax
os.cpus() Returns
object[] — Each entry has model, speed, and times (user/sys/idle).
Examples
import { cpus } from 'node:os';
console.log(cpus().length, 'cores');
Output
8 cores
import { cpus } from 'node:os';
import cluster from 'node:cluster';
if (cluster.isPrimary) {
for (let i = 0; i < cpus().length; i++) cluster.fork();
}
Notes
Commonly used to size a worker pool or cluster. In containers
`cpus().length` reports host cores, not the cgroup CPU limit;
consider `os.availableParallelism()` (Node 19.4+) for that.