Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use process.hrtime to get execution time of async function

Tags:

I am triing to get execution time of async function. Seemingly I can use process.hrtime for this. I created simple example:

console.log("starting"); var start = process.hrtime(); console.log("start"); console.log(start);  setTimeout(function(){     console.log("HELLO");     var end = process.hrtime();     console.log("end");     console.log(end); }, 1000); 

It outputs

starting start [ 131806, 731009597 ] HELLO end [ 131807, 738212296 ] 

But I don't understand where is exectuion time in miliseconds? I expect to get 1000 ms in this example.

like image 525
Maxim Yefremov Avatar asked Aug 03 '13 10:08

Maxim Yefremov


People also ask

What is process Hrtime?

The process. hrtime() method to measure code execution time which returns array which include current high-resolution real time in a [seconds, nanoseconds].

How does node JS calculate execution time?

In order to measure code execution time you have to provide time returned by the first process. hrtime() call, as a parameter to the second process. hrtime() call.


1 Answers

Got it:

console.log("starting"); var start = process.hrtime(); console.log("start"); console.log(start);  setTimeout(function(){     console.log("HELLO");     var end = process.hrtime(start);     console.log("end");     console.log(end); }, 1000); 

Prints

starting start [ 132798, 207101051 ] HELLO end [ 1, 7001730 ] 

That means 1 second and 7001730 nanoseconds from start to end

like image 109
Maxim Yefremov Avatar answered Sep 21 '22 00:09

Maxim Yefremov