Files
James M Snell b9e257f5cb perf_hooks: add histogram.snapshot()
Cloning a histogram with `structuredClone()` or `postMessage()` shares
the native histogram instead of copying it. Capturing the state of a
histogram at a point in time, while other code keeps recording into
it, requires serializing it with `export()` and parsing the result
with `importHistogram()`.

Add `histogram.snapshot()`, which returns a new, independent
`Histogram` containing a copy of the histogram's configuration,
recorded values, `exceeds` count, and EWMA state, without the
serialization round trip. Values cannot be recorded into the returned
histogram. The method is available on all histograms, including
`RecordableHistogram` and `ELDHistogram` instances.

Signed-off-by: James M Snell <jasnell@gmail.com>
Assisted-by: OpenCode
PR-URL: https://github.com/nodejs/node/pull/66099
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2026-09-25 03:31:51 +00:00

25 lines
577 B
JavaScript

'use strict';
const assert = require('assert');
const common = require('../common.js');
const { createHistogram } = require('perf_hooks');
const bench = common.createBenchmark(main, {
n: [1e3],
highest: [1e6, Number.MAX_SAFE_INTEGER],
figures: [2, 3],
});
let snapshot;
function main({ n, highest, figures }) {
const histogram = createHistogram({ highest, figures });
for (let i = 1; i <= 1e4; i++) histogram.record(i);
bench.start();
for (let i = 0; i < n; i++) snapshot = histogram.snapshot();
bench.end(n);
assert.strictEqual(snapshot.count, 1e4);
}