• Node.js Video Tutorials

Node.js - os.loadavg() Method



The os module of Node.js provides a bunch of operating system-related utility methods and properties.

The Node.js os.loadavg() method will return an array containing load averages of 1, 5, and 15 minutes. The load average is a measure of system activity and it will be calculated by the operating system and it'll be displayed as a fractional number.

The load average is a Unix-specific concept. When os.loadavg() method is compiled on the windows operating system it will always return the resultant array as [0, 0, 0].

Syntax

Following is the example of the Node.js os.loadavg() method

os.loadavg()

Parameters

This method does not accept any parameters.

Return value

This method will return a number array containing the 1, 5, and 15 minute load averages of the system activity.

Example

In the following example, we are trying to execute the Node.js os.loadavg() method on WINDOWS operating system.

const os = require('os');
console.log(os.loadavg());

Output

[ 1.70947265625, 3.12890625, 3.38720703125 ]

Note − To get the accurate result, better execute the above code in local.

If we compile and run the above program, the os.loadavg() method will return a number array as [0, 0, 0] because, it is compiled on windows operating system and on windows the return value is always [0, 0, 0].

[ 0, 0, 0 ]

Example

In the example below, we are trying to log the 1, 5, and 15 minute load averages of the system activity on WINDOWS operating system.

const os = require('os');
var load_avg = os.loadavg();

function func() {
   console.log("The average Load of 1 minute in windows  is :" + load_avg[0]);  
   console.log("The average Load of 5 minute in windows is :" + load_avg[1]);  
   console.log("The average Load of 15 minute in windows is :" + load_avg[2]);
}
func();

Output

The average Load of 1 minute in windows is :1.87060546875
The average Load of 5 minute in windows is :2.28466796875
The average Load of 15 minute in windows is :2.71630859375

Note − To get the accurate result, better execute the above code in local.

After executing the above program on windows operating system, the os.loadavg() method logged the 1, 5, and 15 minute load averages of the system activity as 0, 0, and 0.

The average Load of 1 minute in windows is :0
The average Load of 5 minute in windows is :0
The average Load of 15 minute in windows is :0

Example

In the example below, we are trying to execute the os.loadavg() method on LINUX operating system.

const os = require('os');
console.log(os.loadavg());

Output

If we compile and run the above program on LINUX operating system, the os.loadavg() method will return a number array of some fractional numbers as seen in the below output.

[ 2.0380859375, 2.5341796875, 3.0341796875 ]

Example

In the example below, we are trying to log the 1, 5, and 15-minute load averages of the system activity on LINUX operating system.

const os = require('os');

var load_avg = os.loadavg();

function func() {
   console.log("The average Load of 1 minute in LINUX is :" + load_avg[0]);
  
   console.log("The average Load of 5 minute in LINUX is :" + load_avg[1]);
  
   console.log("The average Load of 15 minute in LINUX is :" + load_avg[2]);
}

func();

Output

The average Load of 1 minute in LINUX is :1.73193359375
The average Load of 5 minute in LINUX is :2.43408203125
The average Load of 15 minute in LINUX is :2.9912109375
nodejs_os_module.htm
Advertisements