Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Web APIs in Node.js

Node.js performance.mark() and performance.measure()

Reference for performance.mark() and performance.measure() in Node.js: user timing entries, detail data, measuring between marks, clearing and reading them.

performance.mark(name) records a named point in time on the global performance timeline. performance.measure(name, start, end) records the time between two such points as a measure entry. Together they are the User Timing API: a standard way to label the stages of your own code and read the durations afterwards.

Both functions live on the global performance object and on the one from node:perf_hooks. Timestamps are high-resolution milliseconds relative to the process start (performance.timeOrigin).

NODE
performance.mark('start');
// ... work ...
performance.mark('end');
const m = performance.measure('work', 'start', 'end');

Try it

Node.js Marks and a measure between them

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 Options, defaults and clearing

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.mark(name[, options]);                         // PerformanceMark
performance.measure(name[, startMarkOrOptions[, endMark]]); // PerformanceMeasure
performance.clearMarks([name]);
performance.clearMeasures([name]);
performance.getEntriesByName(name[, type]);
performance.getEntriesByType(type);
Name Description
name Label for the entry. Names need not be unique; several marks can share one.
options.startTime mark() only: use this timestamp instead of the current time.
options.detail Any structured-cloneable value attached to the entry (entry.detail).
startMarkOrOptions A mark name, or an object with start, end, duration and detail. start and end may be mark names or timestamps.
endMark Mark name or timestamp for the end, when the second argument is a name.

Returns: the created PerformanceMark or PerformanceMeasure, each with name, entryType, startTime, duration (always 0 for a mark) and detail.

Errors: measure() throws a SyntaxError (a DOMException, code 12) when a mark name does not exist. Conflicting options (for example start, end and duration all at once) throw a TypeError with code ERR_PERFORMANCE_MEASURE_INVALID_OPTIONS.

Usage notes

  • Same name, several marks. When a name is used more than once, measure() refers to the most recent mark with that name.
  • Measures between marks are cheap, but getEntriesByName() scans the buffer. Do not call it in a hot loop.
  • Entries accumulate. Each call adds to the global timeline, so long-running servers should call clearMarks() and clearMeasures() or read entries through a PerformanceObserver and discard them.
  • Mark entries stay on the timeline even after a measure uses them; clearing is explicit.
  • detail is cloned. Changing the object later does not change the stored copy.
  • Not a benchmark tool. Single measurements are noisy because of JIT warm-up and garbage collection. Repeat the work and look at distributions for performance tests.
  • Prefer performance.now() when you only need one number: it avoids creating entries.