Last updated: Apr 5, 2024
Reading timeยท3 min
os.availableParallelism()
method to calculate the amount of parallelismTo get the number of CPU cores in Node.js:
os
module.os.cpus()
method to get an array of objects containing information
about each logical CPU core.length
property to get the total number of CPU cores.import os from 'os'; // ๐๏ธ if you use CommonJS require() // const os = require('os') const cpuCores = os.cpus(); console.log(cpuCores); const numberOfCPUCores = cpuCores.length; console.log(numberOfCPUCores); // ๐๏ธ 12
The code sample above uses the ES6 import/export syntax.
If you use the CommonJS require()
syntax, use the following import statement
instead.
const os = require('os')
We used the os.cpus method to get an array of objects containing information about each logical CPU core.
The last step is to access the length
property on the array to get the total
number of CPU cores.
import os from 'os'; const cpuCores = os.cpus(); console.log(cpuCores); const numberOfCPUCores = cpuCores.length; console.log(numberOfCPUCores); // ๐๏ธ 12
The output of logging the cpuCores
variable will look something like this.
[ { model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz', speed: 4157, times: { user: 41344220, nice: 77410, sys: 10199960, idle: 781266980, irq: 0 } }, { model: 'Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz', speed: 4139, times: { user: 62238500, nice: 105090, sys: 11682650, idle: 763669540, irq: 0 } } ]
Each CPU info object contains the following properties:
model
- A string representing the model of the CPU core.speed
- The speed of the CPU core in MHz.times
- an object containing the following properties:ueser
- the number of milliseconds the CPU has spent in user mode.nice
- the number of milliseconds the CPU has spent in nice mode.sys
- the number of milliseconds the CPU has sent in sys mode.idle
- the number of milliseconds the CPU has spent in idle mode.irq
- the number of milliseconds the CPU has spent in irq mode.Note that nice
values are POSIX-only. On Windows, the nice
values of all
processors will always be 0
.
os.availableParallelism()
method to calculate the amount of parallelismNote that the os.cpus().length
should not be used to calculate the amount of
parallelism available to a Node.js application.
Instead, you can use the os.availableParallelism method.
import os from 'os'; // ๐๏ธ if you use CommonJS require() // const os = require('os') const parallelism = os.availableParallelism(); console.log(parallelism); // ๐๏ธ 12
The os.availableParallelism()
method returns an integer that represents an
estimate of the default amount of parallelism a program should use.
The return integer is always a value that is greater than 0
.
You can also use the Node.js CLI to get the number of CPU cores.
node -e "console.log(require('os').cpus().length)"
Make sure to alternate between double and single quotes as shown in the example.
If the command is wrapped in double quotes, use single quotes within.
You can learn more about the related topics by checking out the following tutorials: