The os.loadavg() method is an inbuilt application programming interface of the os module which is used to get load average. Load average is a measure of system activity, expressed in fractional number, calculated by operating system.
Syntax:
os.loadavg()
Parameters: This method does not accept any parameters.
Return Value: This method returns an array containing fractional number of size three signifies 1, 5 and 15 minutes load average calculated by the operating system. On windows, it will return [0, 0, 0] as load average is Unix-Specific concept.
Below examples illustrate the use of os.loadavg() method in Node.js:
Example 1:
// Node.js program to demonstrate the // os.loadavg() method // Run in Linux system // Require os module const os = require( 'os' ); // Printing os.loadavg() value console.log(os.loadavg()); |
Output:
[ 13.42041015625, 12.95166015625, 12.72509765625 ]
Example 2:
// Node.js program to demonstrate the // os.loadavg() method // Run on Windows system // Require os module const os = require( 'os' ); // Printing os.loadavg() value console.log(os.loadavg()); |
Output:
[ 0, 0, 0 ]
Example 3:
// Node.js program to demonstrate the // os.loadavg() method // Require os module const os = require( 'os' ); // Printing os.loadavg() value var avg_load = os.loadavg(); console.log( "Load average (1 minute):" + String(avg_load[0])); console.log( "Load average (5 minute):" + String(avg_load[1])); console.log( "Load average (15 minute):" + String(avg_load[2])); |
Output:
Load average (1 minute):15.87158203125 Load average (5 minute):14.193359375 Load average (15 minute):13.365234375
Note: The above program will compile and run by using the node index.js
command.
Reference: https://nodejs.org/api/os.html#os_os_loadavg