How to get the Number of CPU Cores using Node.js [3 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
3 min

banner

# Table of Contents

  1. How to get the Number of CPU Cores using Node.js
  2. Using the os.availableParallelism() method to calculate the amount of parallelism
  3. Get the Number of CPU Cores using the CLI in Node.js

# How to get the Number of CPU Cores using Node.js

To get the number of CPU cores in Node.js:

  1. Import the os module.
  2. Use the os.cpus() method to get an array of objects containing information about each logical CPU core.
  3. Access the length property to get the total number of CPU cores.
index.js
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

get number of cpu cores

The code for this article is available on GitHub

The code sample above uses the ES6 import/export syntax.

If you use the CommonJS require() syntax, use the following import statement instead.

index.js
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.

index.js
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.

index.js
[ { 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.

# Using the os.availableParallelism() method to calculate the amount of parallelism

Note 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.

index.js
import os from 'os'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() // const os = require('os') const parallelism = os.availableParallelism(); console.log(parallelism); // ๐Ÿ‘‰๏ธ 12

calculate amount of parallelism

The code for this article is available on GitHub

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.

# Get the Number of CPU Cores using the CLI in Node.js

You can also use the Node.js CLI to get the number of CPU cores.

shell
node -e "console.log(require('os').cpus().length)"

using cli to get total number of cpu cores in node js

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev