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.
The process. hrtime() method to measure code execution time which returns array which include current high-resolution real time in a [seconds, nanoseconds].
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With