Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js process.hrtime( ) Method

  • Last Updated : 29 Sep, 2021

The process.hrtime() method to measure code execution time which returns array which include current high-resolution real time in a [seconds, nanoseconds]. We measure the code execution time by providing the time returned by the first process.hrtime() call as a parameter in the second process.hrtime() call.

The Advantage of process.hrtime() is it measures time very accurate execution time which last less than a millisecond.

Syntax:

process.hrtime([time])

Parameter: This method accept a single parameter as mentioned above an described below.

  • time : The time is an optional parameter that must be the result of a previous process.hrtime() call to difference with the current time.

Return Type: It returns an array of 2 ints. The 1. int contains the seconds and the 2. int the nanoseconds. These times are relative to an arbitrary time in the past, and not related to the time of day.

Example 1:

Javascript




// Implement the function..
 
var hrTime = process.hrtime()
 
// Time in millisecond...
console.log("Time in millisecond is: ", hrTime[0] * 1000 + hrTime[1] / 1000000)

 
Output:
 

Time in millisecond is:  218394926745.5

 

Example 2:

 

Javascript




// Create a variable and call the process.hrtime() function.
var start_time = process.hrtime();
 
 
 
// Print the Start time:
console.log("Start Time:",start_time);
 
// Make the add function
setTimeout(function(){
 
// Create two variable
var a = '40',
b = '50';
 
// Print the Addition result:
console.log("Add of two number is :",(a - 0) + (b - 0));
  
 
    // Create a variable and call the second process.hrtime()
    // function and pass the start time as parameter.
    var end_time = process.hrtime(start_time);
    // Print the Execution time.
    console.log("End Time:",end_time);
     
}, 1000);

Output: It means 1 second and 8779100 nanoseconds from start to end time is taken.

Start Time: [ 682340, 452477300 ]
Add of two number is : 90
End Time: [ 1, 8779100 ]

Example 3:

Javascript




// Create a variable and call the process.hrtime() function.
var start_time = process.hrtime();
 
 
 
// Print the Start time:
console.log("Start Time:",start_time);
 
// Make the add function
setTimeout(function(){
 
    console.log("Execution time will be calculated"+
                " for printing this message....");
 
    // Create a variable and call the second process.hrtime()
    // function and pass the start time as.
    var end_time = process.hrtime(start_time);
    // Print the Execution time.
    console.log("End Time:",end_time);
     
}, 1000);

 
 

Output: It means 1 second and 10987200 nanoseconds from start to end time is taken.

 

Start Time: [ 682865, 516565300 ]
Execution time will be calculated for printing this message....
End Time: [ 1, 10987200 ]

 

Reference Taken : https://nodejs.org/api/process.html#process_process_hrtime_time

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!