Measure elapsed execution time
Benchmarking synchronous or asynchronous code paths.
Node.js · Module
node:perf_hooks provides browser-compatible performance APIs and Node.js-specific measurements. It supports high-resolution timing, user and resource timing, observers, function instrumentation, and event-loop utilization.
The module exposes browser-aligned Performance APIs plus Node-specific measurements. Use the shared performance object for high-resolution timestamps, user-timing marks and measures, timeline queries, resource timing, function instrumentation, and event-loop utilization.
const perf_hooks = require('node:perf_hooks');| Name | Type | Description |
|---|---|---|
fn | Function | Function to instrument with performance timing. |
name | string | Name assigned to a user-timing mark. |
options | PerformanceMarkOptions | Optional mark configuration. |
startMarkOrOptions | string | PerformanceMeasureOptions | Optional first mark or measure configuration. |
endMark | string | Optional ending mark name. |
The module exposes performance measurement APIs, including a performance object, timing classes, and PerformanceObserver. Individual methods return timestamps, performance entries, entry collections, utilization objects, or wrapped functions as applicable.
node:perf_hooks implements selected Web Performance APIs and adds measurements specific to Node.js.performance object is the main interface for collecting metrics from the running Node.js instance.performance.mark(name) records a named point that can later be queried or used by a measure.const { performance } = require('node:perf_hooks');
const start = performance.now();
for (let i = 0; i < 1_000_000; i++);
const elapsed = performance.now() - start;
console.log(`Elapsed: ${elapsed.toFixed(2)} ms`);Measures elapsed wall-clock time with a high-resolution timestamp.
Benchmarking synchronous or asynchronous code paths.
Tracking named application phases with timeline entries.
Diagnosing event-loop saturation in a server.
Collecting execution durations for selected functions.
performance.now() measure?performance.now() returns a high-resolution millisecond timestamp relative to the start of the current Node.js process. Subtracting two readings gives an elapsed duration without depending on the system wall clock.
Follow-up: How would you time a named operation? Use performance.mark() before and after it, then call performance.measure() and retrieve the resulting entry from the Performance Timeline.
PerformanceObserver receives notifications when new PerformanceEntry objects are added to the Performance Timeline. The observer must be configured with the entry types relevant to the measurements being collected.
Follow-up: How can function execution be observed? Wrap the function with performance.timerify() and observe the resulting function entries using a PerformanceObserver subscribed to the function type.
eventLoopUtilization() reports cumulative active and idle event-loop time in high-resolution milliseconds. Passing earlier utilization values lets it calculate utilization over an interval rather than only since process start.
Follow-up: Why use interval values? Comparing snapshots isolates a measurement window and avoids confusing lifetime utilization with the period being investigated.
performancePerformanceObserverPerformanceEntryPerformanceMarkPerformanceMeasureperformance.markperformance.measureperformance.now