I think the question is straight forward.
I'm looking for something that's similar to window.performance.now() in nodejs V8 engine.
Right now I'm just using:-
var now = Date.now();
//do some processing..
console.log("time elapsed:", Date.now() - now);
But, I read that window.performance.now() is lot more accurate than using the date because of the what's defined here.
Node v8.5.0 has added Performance Timing API, which includes the
performance#now()
, e.g.What about?
Here's a shortcut for
process.hrtime()
that returns milliseconds instead of microseconds:Usage:
Will output something like "Took 200ms"
I would only mention that three of the reasons the author gives for the preference of the timing API in the browser wouldn't seem to apply directly to a node situation, and the fourth, the inaccuracy of Javscript time, cites an article from 2008, and I would strongly caution against relying on older material regarding Javascript performance specifics, particularly given the recent round of performance improvements all the engines have made to support "HTML5" apps.
However, in answer to your question, you should look at
process.hrtime()
UPDATE: The
present
package (available vianpm install present
) provides some sugar aroundhrtime
if you'd like it.Here's a Typescript version with process.hrtime(), based on NextLocal's answer:
Usage: