Node.js os.loadavg() Method
The os.loadavg() method is an inbuilt application programming interface of the os module which is used to get the load average. Load average is a measure of system activity, expressed in a fractional number, calculated by the operating system.
Syntax:
os.loadavg()
Parameters: This method does not accept any parameters.
Return Value: This method returns an array containing a 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 the load average is a Unix-Specific concept.
Example 1: The below example illustrates the use of the os.loadavg() method in Node.js:
javascript
// 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: The below example illustrates the use of the os.loadavg() method in Node.js:
javascript
// 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: The below example illustrates the use of os.loadavg() method in Node.js:
javascript
// Node.js program to demonstrate the // os.loadavg() method // Require os module const os = require( 'os' ); // Printing os.loadavg() value let 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
Contact Us