Benchmark an operation
Measure the duration of synchronous or asynchronous application work.
Node.js · Const
Node.js’s performance object provides high-resolution timing and Performance Timeline operations for measuring code and inspecting runtime metrics.
The performance object from node:perf_hooks exposes high-resolution timing and the Performance Timeline for the current Node.js process. Record marks, create measures between them, inspect entries, and clear entries when they are no longer needed.
import { performance } from 'node:perf_hooks';| Name | Type | Description |
|---|---|---|
name | string | Name of the mark or measure entry; measure calls may also accept start and end mark names. |
startMark | string | Optional starting mark name or timing point for a measure. |
endMark | string | Optional ending mark name or timing point for a measure. |
A Performance object that provides timing methods and access to entries in the Performance Timeline.
performance is the central object for collecting timing information from the running Node.js process.performance.now() gives a high-resolution elapsed-time value in milliseconds relative to process start.performance.mark() to record named points that can later define measurement boundaries.performance.measure() to create a duration entry between marks or other supported timing points.import { performance } from 'node:perf_hooks';
performance.mark('start');
await new Promise((resolve) => setTimeout(resolve, 20));
performance.mark('end');
performance.measure('work', 'start', 'end');
const [entry] = performance.getEntriesByName('work');
console.log(`${entry.name}: ${entry.duration.toFixed(2)} ms`);Creates marks around asynchronous work, measures the interval, and prints the resulting duration.
Measure the duration of synchronous or asynchronous application work.
Mark phases such as initialization, request handling, and shutdown.
Collect and inspect timing entries for diagnostics or instrumentation.
PerformanceEntryPerformanceMarkPerformanceMeasurePerformanceObserverperformance.mark()performance.measure()performance.now()performance.getEntriesByName()