Get the Number of CPU Cores in Node.js

Updated: July 12, 2022 By: A Goodman Post a comment

The standard module os of Node.js provides a lot of operating system-related utilities. The os.cpus() method returns an array of objects containing information about each logical CPU core. To count the number of CPU cores, we can use:

os.cpus().length

Example:

import os from 'os'

// For CommonJS project, use:
// const os = require('os');

const coreNumber = os.cpus().length;

// Printing the number of CPU cores
console.log(coreNumber);        

My output (yours might be different from mine):

8

Further reading:

You can also check out our Node.js category page or PHP category page for the latest tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Articles