Performance monitoring is an important part of building reliable Node.js applications. A server may respond quickly under normal traffic but experience delays when the event loop becomes busy, database operations slow down, or background tasks consume too many resources. Looking at a single performance measurement does not always reveal when the problem started or whether the application is getting better or worse.
Node.js provides the Performance Hooks API for collecting performance measurements. One useful feature is the Histogram API, which records values and helps developers understand their distribution through statistics such as minimum, maximum, mean, and percentiles.
Node.js also provides histogram.diff(), a method that compares two histogram snapshots and returns the measurements recorded between them. It allows applications to monitor performance over individual time intervals without repeatedly resetting the original histogram.
The method is available in Node.js 24.23.0 and Node.js 26.11.0. It is particularly useful when monitoring event loop delays, measuring application latency, and building interval-based performance reports.
This article explains how histogram.diff() works, how to create and compare snapshots, and how to use it in a practical Node.js monitoring application.
Understanding Histograms in Node.js
A histogram is a statistical structure that groups recorded measurements into ranges called buckets. Instead of storing only a single average, it maintains information about how frequently different values occur.
For example, consider an API that records response times for incoming requests:
Request response times:
12 ms
15 ms
18 ms
22 ms
25 ms
30 ms
45 ms
80 ms
120 ms
250 msCalculating the average response time gives one useful measurement, but it does not explain the complete distribution. Most requests may finish quickly while a small number take considerably longer.
A histogram helps reveal this behavior.
Node.js provides histogram functionality through the node:perf_hooks module. You can create a histogram using createHistogram() or monitor event loop delays using monitorEventLoopDelay().
For example:
const { createHistogram } = require('node:perf_hooks');
const histogram = createHistogram();
histogram.record(12000000);
histogram.record(15000000);
histogram.record(25000000);
histogram.record(80000000);
console.log('Count:', histogram.count);
console.log('Minimum:', histogram.min);
console.log('Maximum:', histogram.max);
console.log('Mean:', histogram.mean);
console.log('P99:', histogram.percentile(99));The recorded values in this example are expressed in nanoseconds.
A histogram provides more information than a simple counter or average. It helps developers understand the distribution of measurements and identify unusually high values.
What Is histogram.diff()?
The histogram.diff() method compares the current histogram with an earlier snapshot and returns a new histogram containing the measurements recorded after that snapshot was taken.
Its syntax is:
currentHistogram.diff(previousHistogram)The method accepts one argument:
Parameter | Description |
|---|---|
other | An earlier histogram snapshot used as the comparison baseline |
The method returns a new Histogram object. Neither the current histogram nor the earlier snapshot is modified.
For example, suppose an application records performance measurements continuously.
At the beginning of a monitoring interval, you take a snapshot. After one minute, you take another snapshot. Calling diff() on the newer snapshot returns the measurements collected during that interval.
This is useful because you do not need to reset the original histogram every time you want an interval-based report.
The difference histogram contains the changes in:
Total recorded values.
Values exceeding the configured range.
Histogram bucket counts.
Minimum and maximum values calculated from the difference buckets.
The returned histogram does not retain EWMA state, and its resetCount is zero.
Creating a Histogram Snapshot
The snapshot() method creates an independent copy of a histogram's current state.
Consider the following example:
const { createHistogram } = require('node:perf_hooks');
const histogram = createHistogram();
histogram.record(10000000);
histogram.record(20000000);
const firstSnapshot = histogram.snapshot();
histogram.record(30000000);
histogram.record(40000000);
const secondSnapshot = histogram.snapshot();
console.log('First snapshot count:', firstSnapshot.count);
console.log('Second snapshot count:', secondSnapshot.count);The output is:
First snapshot count: 2
Second snapshot count: 4The first snapshot contains two measurements. The second snapshot contains all four measurements recorded by that point.
The important detail is that taking a snapshot does not reset the original histogram.
The original histogram continues collecting measurements, while each snapshot preserves the state that existed when it was created.
This makes snapshots useful as comparison points for performance monitoring.
Comparing Snapshots With histogram.diff()
Now that you understand snapshots, you can use diff() to calculate the measurements recorded between two points.
Consider this example:
const { createHistogram } = require('node:perf_hooks');
const histogram = createHistogram();
histogram.record(10000000);
histogram.record(20000000);
const previous = histogram.snapshot();
histogram.record(30000000);
histogram.record(40000000);
histogram.record(50000000);
const current = histogram.snapshot();
const difference = current.diff(previous);
console.log('Previous count:', previous.count);
console.log('Current count:', current.count);
console.log('Difference count:', difference.count);
console.log('Difference minimum:', difference.min);
console.log('Difference maximum:', difference.max);The output is:
Previous count: 2
Current count: 5
Difference count: 3
Difference minimum: 30000000
Difference maximum: 50000000The original histogram contains five measurements. The earlier snapshot contains two. The difference histogram contains only the three measurements recorded after the earlier snapshot.
This is the main purpose of histogram.diff().
Instead of comparing total counts manually, you receive a new histogram containing the interval's measurement distribution. You can then inspect its percentiles, mean, minimum, maximum, and other available statistics.
Monitoring Event Loop Delays With histogram.diff()
One of the most practical uses of histogram.diff() is monitoring event loop delays.
Node.js uses an event loop to manage asynchronous operations. When JavaScript executes a long-running synchronous operation, the event loop cannot process other callbacks until that work completes.
This can increase request latency, delay timers, and affect the responsiveness of the application.
Node.js provides monitorEventLoopDelay() to measure event loop delay.
The following example monitors event loop delays and reports the 99th percentile every ten seconds:
const {
monitorEventLoopDelay
} = require('node:perf_hooks');
const histogram = monitorEventLoopDelay({
resolution: 20
});
histogram.enable();
let previous = histogram.snapshot();
const interval = setInterval(() => {
const current = histogram.snapshot();
const delta = current.resetCount === previous.resetCount
? current.diff(previous)
: current;
console.log({
count: delta.count,
minimumMs: delta.min / 1e6,
maximumMs: delta.max / 1e6,
meanMs: delta.mean / 1e6,
p99Ms: delta.percentile(99) / 1e6
});
previous = current;
}, 10000);
function shutdown() {
clearInterval(interval);
histogram.disable();
process.exit(0);
}
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);This example performs several important operations.
First, monitorEventLoopDelay() creates a histogram that records event loop delay measurements. The resolution option controls the sampling resolution in milliseconds.
Next, the application takes an initial snapshot. This snapshot becomes the baseline for the first reporting interval.
Every ten seconds, the application takes another snapshot and calculates the difference. The resulting histogram contains measurements collected during that interval.
The application then reports the count, minimum, maximum, mean, and 99th percentile.
The values returned by monitorEventLoopDelay() are in nanoseconds. Dividing by one million converts them into milliseconds.
The resetCount check is important. If the original histogram has been reset between snapshots, diff() cannot calculate the difference. In that situation, the example uses the current snapshot as the new reporting result.
Finally, the application updates the baseline so that the next report covers only the next interval.
Why Interval-Based Statistics Are Useful
A histogram that continuously accumulates measurements provides information about the complete monitoring period. However, it may hide short periods of poor performance.
Consider an application that has been running for several hours.
Its overall 99th percentile may look acceptable because most of the measurements were collected during normal operation. A five-minute performance problem may be hidden by the larger collection of earlier measurements.
Interval-based statistics provide a more detailed view.
For example:
Monitoring interval | Event loop delay P99 |
|---|---|
Interval 1 | 12 ms |
Interval 2 | 15 ms |
Interval 3 | 18 ms |
Interval 4 | 95 ms |
Interval 5 | 110 ms |
The increase in the fourth and fifth intervals indicates that the application experienced higher event loop delays during those periods.
With histogram.diff(), developers can inspect each interval independently while keeping the original histogram active.
This makes it easier to identify when performance changes occur instead of relying only on application-wide averages.

Join the conversation! Your thoughts help the community grow.