Node.js Reference ยท node:sqlite and Runtime Services
Node.js performance.now() method
Reference for performance.now() in Node.js: a monotonic, sub-millisecond clock for measuring durations, how it differs from Date.now() and process.hrtime().
performance.now() returns a number of milliseconds, with a fractional part, measured from the moment the current process started timing (its timeOrigin). The clock is monotonic: it only moves forward and is not affected by the user or an NTP service adjusting the system time. That makes it the right tool for measuring how long something took.
The value on its own means little. You take one reading before the work, another after, and subtract. It is available as a global and from node:perf_hooks.
const t0 = performance.now(); // ... work ... const elapsedMs = performance.now() - t0;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
performance.now(): number
This method takes no parameters.
Returns: milliseconds as a double since performance.timeOrigin. Sub-millisecond precision is available, though it is not guaranteed to be nanosecond-accurate.
Errors: none in normal use. Keep the call on the object: pulling the method off (const { now } = performance) and calling it alone is not supported.
Usage notes
- Use it for durations, not dates.
Date.now()follows the wall clock and can jump;performance.now()cannot. To get a date, addperformance.timeOrigin. - Compared with
process.hrtime.bigint():hrtimereturns nanoseconds as aBigIntfrom an arbitrary starting point;now()returns fractional milliseconds from process start. Both are monotonic. Usenow()for readable results andhrtime.bigint()when you want integer nanoseconds. - Timing async code: read
now()before and after anawait; the difference includes time spent waiting for other callbacks. - A single measurement of a tiny operation is noise. Repeat it many times, discard warm-up runs and compare medians or best results.
- Worker threads get their own origin. In a worker,
now()counts from when the worker started, so do not compare readings across threads without adding eachtimeOrigin. - Marks and measures build on the same clock:
performance.mark('a')storesnow()under a name.