Skip to content

Commit 645be73

Browse files
jasnellevanlucas
authored andcommitted
perf_hooks,http2: add performance.clear()
Add missing clear() method to `perf_hooks.performance` to remove the entries from the master timeline to prevent that from being a memory leak. PR-URL: #18046 Reviewed-By: Matteo Collina <[email protected]>
1 parent 30e2221 commit 645be73

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/api/perf_hooks.md

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ added: v8.5.0
2929
The `Performance` provides access to performance metric data. A single
3030
instance of this class is provided via the `performance` property.
3131

32+
### performance.clearEntries(name)
33+
<!-- YAML
34+
added: REPLACEME
35+
-->
36+
37+
Remove all performance entry objects with `entryType` equal to `name` from the
38+
Performance Timeline.
39+
3240
### performance.clearFunctions([name])
3341
<!-- YAML
3442
added: v8.5.0

lib/perf_hooks.js

+4
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,10 @@ class Performance extends PerformanceObserverEntryList {
471471
this[kClearEntry]('function', name);
472472
}
473473

474+
clearEntries(name) {
475+
this[kClearEntry](name);
476+
}
477+
474478
timerify(fn) {
475479
if (typeof fn !== 'function') {
476480
const errors = lazyErrors();

test/parallel/test-http2-perf_hooks.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if (!common.hasCrypto)
66
const assert = require('assert');
77
const h2 = require('http2');
88

9-
const { PerformanceObserver } = require('perf_hooks');
9+
const { PerformanceObserver, performance } = require('perf_hooks');
1010

1111
const obs = new PerformanceObserver(common.mustCall((items) => {
1212
const entry = items.getEntries()[0];
@@ -46,6 +46,7 @@ const obs = new PerformanceObserver(common.mustCall((items) => {
4646
default:
4747
assert.fail('invalid entry name');
4848
}
49+
performance.clearEntries('http2');
4950
}, 4));
5051
obs.observe({ entryTypes: ['http2'] });
5152

@@ -100,3 +101,10 @@ server.on('listening', common.mustCall(() => {
100101
}));
101102

102103
}));
104+
105+
process.on('exit', () => {
106+
const entries = performance.getEntries();
107+
// There shouldn't be any http2 entries left over.
108+
assert.strictEqual(entries.length, 1);
109+
assert.strictEqual(entries[0], performance.nodeTiming);
110+
});

0 commit comments

Comments
 (0)