From 7c0b8ca5ac5181f098fb12dbd9f5c4ea88de3018 Mon Sep 17 00:00:00 2001 From: Xuguang Mei Date: Sat, 26 Feb 2022 16:46:59 +0800 Subject: [PATCH] perf_hooks: do not return all entries with getEntriesBy[Name|Type] Fix: https://github.com/nodejs/node/issues/42028 PR-URL: https://github.com/nodejs/node/pull/42104 Fixes: https://github.com/nodejs/node/issues/42028 Reviewed-By: Mestery Reviewed-By: Benjamin Gruenbaum --- lib/internal/perf/performance.js | 11 +++++++---- test/parallel/test-performance-timeline.mjs | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/internal/perf/performance.js b/lib/internal/perf/performance.js index 38dac0ee32397c..20603fa382e702 100644 --- a/lib/internal/perf/performance.js +++ b/lib/internal/perf/performance.js @@ -9,6 +9,7 @@ const { const { codes: { ERR_ILLEGAL_CONSTRUCTOR, + ERR_MISSING_ARGS } } = require('internal/errors'); @@ -86,16 +87,18 @@ function getEntries() { } function getEntriesByName(name) { - if (name !== undefined) { - name = `${name}`; + if (arguments.length === 0) { + throw new ERR_MISSING_ARGS('name'); } + name = `${name}`; return filterBufferMapByNameAndType(name, undefined); } function getEntriesByType(type) { - if (type !== undefined) { - type = `${type}`; + if (arguments.length === 0) { + throw new ERR_MISSING_ARGS('type'); } + type = `${type}`; return filterBufferMapByNameAndType(undefined, type); } diff --git a/test/parallel/test-performance-timeline.mjs b/test/parallel/test-performance-timeline.mjs index 18ef762ffd8d3b..57e0f6f0b7b6da 100644 --- a/test/parallel/test-performance-timeline.mjs +++ b/test/parallel/test-performance-timeline.mjs @@ -33,3 +33,18 @@ await setTimeout(50); performance.measure('a', 'one'); const entriesByName = performance.getEntriesByName('a'); assert.deepStrictEqual(entriesByName.map((x) => x.entryType), ['measure', 'mark', 'measure', 'mark']); + +// getEntriesBy[Name|Type](undefined) +performance.mark(undefined); +assert.strictEqual(performance.getEntriesByName(undefined).length, 1); +assert.strictEqual(performance.getEntriesByType(undefined).length, 0); +assert.throws(() => performance.getEntriesByName(), { + name: 'TypeError', + message: 'The "name" argument must be specified', + code: 'ERR_MISSING_ARGS' +}); +assert.throws(() => performance.getEntriesByType(), { + name: 'TypeError', + message: 'The "type" argument must be specified', + code: 'ERR_MISSING_ARGS' +});