Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Globals and Console

Node.js performance object

Reference for the global performance object in Node.js: performance.now(), timeOrigin, mark(), measure(), timerify() and eventLoopUtilization().

The global performance object gives you a high-resolution, monotonic clock plus tools to label and measure parts of a program. performance.now() returns the number of milliseconds (with fractions) since the process started, and it never jumps backwards when the system clock is adjusted, which makes it the right tool for timing code.

It is the same API that browsers expose, backed in Node by the node:perf_hooks module. Beyond now(), it can record named marks, measure the time between them, wrap functions to time every call, and report how busy the event loop is.

NODE
const start = performance.now();
// ... work ...
console.log(performance.now() - start >= 0); // true

Try it

Node.js Timing code with now()

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.

Node.js Marks and measures

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

NODE
performance.now(): number
performance.timeOrigin: number
performance.mark(name: string, options?: { detail?: any, startTime?: number }): PerformanceMark
performance.measure(name: string, startMark?: string | object, endMark?: string): PerformanceMeasure
performance.getEntries(), getEntriesByName(name, type?), getEntriesByType(type): PerformanceEntry[]
performance.clearMarks(name?), clearMeasures(name?)
performance.timerify(fn, options?): Function
performance.eventLoopUtilization(prev1?, prev2?): { idle, active, utilization }
performance.toJSON(): object
Name Description
now() Milliseconds since timeOrigin, as a floating-point number, monotonic.
timeOrigin Unix-epoch time in milliseconds when the process timing started.
mark(name) Records a timestamp entry with that name.
measure(name, start, end) Creates an entry whose duration is the time between two marks (or option values). Without marks it spans from the start of the process to now.
timerify(fn) Returns a wrapper that records a function entry for each call, visible to a PerformanceObserver.
eventLoopUtilization() Time the event loop spent idle versus active; pass an earlier result to get the difference.

Returns: timestamps are numbers; mark() and measure() return the new entry.

Errors: measure() throws if a named mark does not exist.

Usage notes

  • Use it instead of Date.now() for durations. Date follows the wall clock, which can be corrected backwards or forwards; performance.now() cannot.
  • Resolution is in fractions of a millisecond, but it is not a profiler: one measurement of a tiny loop is mostly noise. Repeat and compare.
  • Entries pile up. Marks and measures stay in memory until you call clearMarks() or clearMeasures(). In a long-running server, clear them or read them with an observer.
  • getEntries* only return marks and measures created through this object, not everything an observer can see.
  • Differs from process.hrtime(). hrtime.bigint() returns nanoseconds since an arbitrary point; performance.now() is milliseconds since process start. Both are monotonic.
  • Timing async code works the same way: take now() before and after an await.
  • performance is also available as require('node:perf_hooks').performance, which is the explicit form.