diff --git a/doc/api/perf_hooks.md b/doc/api/perf_hooks.md index 01963535c47852..5d3086bd2ef4b8 100644 --- a/doc/api/perf_hooks.md +++ b/doc/api/perf_hooks.md @@ -1973,6 +1973,23 @@ added: v11.10.0 Enables the update interval timer. Returns `true` if the timer was started, `false` if it was already started. +### `histogram[Symbol.dispose]()` + + + +Disables the update interval timer when the histogram is disposed. + +```js +const { monitorEventLoopDelay } = require('node:perf_hooks'); +{ + using hist = monitorEventLoopDelay({ resolution: 20 }); + hist.enable(); + // The histogram will be disabled when the block is exited. +} +``` + ### Cloning an `IntervalHistogram` {IntervalHistogram} instances can be cloned via {MessagePort}. On the receiving diff --git a/lib/internal/perf/event_loop_delay.js b/lib/internal/perf/event_loop_delay.js index 8281ea105f4083..17581b1310c5c0 100644 --- a/lib/internal/perf/event_loop_delay.js +++ b/lib/internal/perf/event_loop_delay.js @@ -3,6 +3,7 @@ const { ReflectConstruct, SafeMap, Symbol, + SymbolDispose, } = primordials; const { @@ -38,7 +39,7 @@ const { const kEnabled = Symbol('kEnabled'); class ELDHistogram extends Histogram { - constructor(i) { + constructor() { throw new ERR_ILLEGAL_CONSTRUCTOR(); } @@ -65,6 +66,10 @@ class ELDHistogram extends Histogram { this[kHandle].stop(); return true; } + + [SymbolDispose]() { + this.disable(); + } } /** diff --git a/test/parallel/test-perf-hooks-histogram.js b/test/parallel/test-perf-hooks-histogram.js index 37fcdfb3fca06c..9e76cca2f4f479 100644 --- a/test/parallel/test-perf-hooks-histogram.js +++ b/test/parallel/test-perf-hooks-histogram.js @@ -97,6 +97,17 @@ const { inspect } = require('util'); }, 50); } +{ + // Tests that the ELD histogram is disposable + let histogram; + { + using hi = monitorEventLoopDelay(); + histogram = hi; + } + // The histogram should already be disabled. + strictEqual(histogram.disable(), false); +} + { const h = createHistogram(); ok(inspect(h, { depth: null }).startsWith('Histogram'));