Node.js process.uptime() Method
Last Updated :
11 Oct, 2021
The
process.uptime() method is an inbuilt application programming interface of the process module which is used to get the number of seconds the Node.js process is running.
Syntax:
process.uptime();
Parameters: This method does not accept any parameter.
Return Value: It returns a floating-point number specifying the number of seconds the Node.js process is running.
Below examples illustrate the use of process.uptime() method in Node.js:
Example 1:
javascript
// Node.js program to demonstrate the
// process.uptime() Method
// Allocating process module
const process = require('process');
// Printing the number of seconds
// the Node.js process is running
console.log(process.uptime());
Output:
0.0842063
Example 2:
javascript
// Node.js program to demonstrate the
// process.uptime() Method
// Allocating process module
const process = require('process');
// Checking whether the method
// exists or not
if (process.uptime) {
// Printing uptime() value
console.log("The number of seconds the"
+ " Node.js process is running: "
+ process.uptime() + " seconds");
var i = 1000000;
while(i--);
console.log("The number of seconds the"
+ " Node.js process is running: "
+ process.uptime() + " seconds");
console.log("In whole seconds: "
+ Math.floor(process.uptime())
+ " seconds");
}
Output:
the number of seconds the Node.js process is running: 0.077 seconds
the number of seconds the Node.js process is running: 0.087 seconds
In whole seconds: 0 seconds
Note: The above program will compile and run by using the
node filename.js
command.
Reference: https://nodejs.org/api/process.html#process_process_uptime