DEV Community

Cover image for Node Js Get Total Number of System CPU Cores Tutorial
Suresh Ramani
Suresh Ramani

Posted on • Originally published at techvblogs.com

Node Js Get Total Number of System CPU Cores Tutorial

In this short tutorial, we will share the quick and straightforward way to get the total number of computer processors through the Node js application using the os module.

To obtain the operating system’s information, we will use the cpus() method, the cpus() method is available through the OS module in node js.

The os.cpus() method is a default module of node js; when invoked, it returns an array of objects holding the information regarding every CPU/Core installed on your system.

This quick tutorial will show how to call the cpus() method and get the model, speed (in MHz), and times that each logical CPU core takes.

The data returns an array with the number of processors incorporated in the system.

Here are the steps we are going to take to accomplish this node js guide.

How to Get System CPU Cores or Processors Available in Node Js

  • Step 1: Create App Directory
  • Step 2: Build Package JSON
  • Step 3: Make App.js File
  • Step 4: Get CPU Cores Numbers
  • Step 5: Display Output in Node

Create App Directory

We are about to create a new folder on your system; we will keep the files and folders related to this small project in this directory.

You may generate a folder using a single command mentioned below:

mkdir node-cpus
Enter fullscreen mode Exit fullscreen mode

let us navigate to the project folder’s root:

cd node-cpus
Enter fullscreen mode Exit fullscreen mode

Build Package JSON

In this step, we will show you how to configure npm in your node project. You must be thinking, why do we need to do this.

We need to run the npm initializer command because we might be needing some external packages or built-in modules to access the os system-related information.

So, after creating a brand new project directory. You must initialize the given npm command.

npm init
Enter fullscreen mode Exit fullscreen mode
{
  "name": "node-cpus",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "dependencies": {},
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
Enter fullscreen mode Exit fullscreen mode

Make App.js File

In this step, you require to create a new file at the root of your project folder, we are naming it app.js.

However, you can name it anything you want, but the app or server sounds more generic.

To know the CPU cores in the node we will be using the command-line interface.

In order to execute the command to see the output, first, you must register the command in the script section in the package.json file.

"scripts": {
   "start": "node app.js"
},
Enter fullscreen mode Exit fullscreen mode

Get CPU Cores Numbers

In this step, you need to go to the app.js file, open the file and insert all the given code into the file.

const os = require('os');

const systemCpuCores = os.cpus();

console.log(systemCpuCores);
Enter fullscreen mode Exit fullscreen mode

Display Output in Node

Head over to command prompt, type the suggested command on the console’s screen then hit enter.

node app.js
Enter fullscreen mode Exit fullscreen mode

After running the command, you will see the model of the total number of processors in your system.

[
  {
    model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz',
    speed: 2700,
    times: {
      user: 4863830,
      nice: 17060,
      sys: 1096340,
      idle: 17975530,
      irq: 0
    }
  },
  {
    model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz',
    speed: 2683,
    times: {
      user: 4639360,
      nice: 15750,
      sys: 1175380,
      idle: 18029760,
      irq: 0
    }
  },
  {
    model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz',
    speed: 2649,
    times: {
      user: 4802690,
      nice: 13770,
      sys: 1096340,
      idle: 17958540,
      irq: 0
    }
  },
  {
    model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz',
    speed: 1817,
    times: {
      user: 5105390,
      nice: 15660,
      sys: 1143620,
      idle: 17753240,
      irq: 0
    }
  }
]
Enter fullscreen mode Exit fullscreen mode

Each CPU object in the array has these values:

  • model: string of the CPU model
  • speed: number of the CPU speed in MHz
  • user: number of milliseconds the CPU has spent in user mode
  • nice: number of milliseconds the CPU has spent in nice mode
  • sys: number of milliseconds the CPU has spent in sys mode
  • idle: number of milliseconds the CPU has spent in idle mode
  • irq: number of milliseconds the CPU has spent in irq mode Given this array of objects, we can get the number of CPUs by getting the length of the array.

Here's what the code would look like:

const numOfCpus = os.cpus().length
console.log(numOfCpus)
Enter fullscreen mode Exit fullscreen mode

When you log the numOfCpus, it will print a number for the amount of CPUs the system your Node.js code is running in has.

Thank you for reading this blog.

Top comments (0)