From a258bc9b70ed52fb36f60de5c2c4b68b67568815 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 31 Dec 2020 12:54:08 +0100 Subject: [PATCH] perf_hooks: refactor to avoid unsafe array iteration PR-URL: https://github.com/nodejs/node/pull/36723 Reviewed-By: James M Snell Reviewed-By: Rich Trott --- lib/perf_hooks.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/perf_hooks.js b/lib/perf_hooks.js index 6733992b9fae39..5cbf018ff8efae 100644 --- a/lib/perf_hooks.js +++ b/lib/perf_hooks.js @@ -3,6 +3,7 @@ const { ArrayIsArray, ArrayPrototypeFilter, + ArrayPrototypeForEach, ArrayPrototypeIncludes, ArrayPrototypeMap, ArrayPrototypePush, @@ -375,13 +376,13 @@ class PerformanceObserver { disconnect() { const observerCountsGC = observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC]; const types = this[kTypes]; - for (const key of ObjectKeys(types)) { + ArrayPrototypeForEach(ObjectKeys(types), (key) => { const item = types[key]; if (item) { L.remove(item); observerCounts[key]--; } - } + }); this[kTypes] = {}; if (observerCountsGC === 1 && observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC] === 0) { @@ -408,14 +409,14 @@ class PerformanceObserver { this[kBuffer][kEntries] = []; L.init(this[kBuffer][kEntries]); this[kBuffering] = Boolean(options.buffered); - for (const entryType of filteredEntryTypes) { + ArrayPrototypeForEach(filteredEntryTypes, (entryType) => { const list = getObserversList(entryType); - if (this[kTypes][entryType]) continue; + if (this[kTypes][entryType]) return; const item = { obs: this }; this[kTypes][entryType] = item; L.append(list, item); observerCounts[entryType]++; - } + }); if (observerCountsGC === 0 && observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC] === 1) { installGarbageCollectionTracking(); @@ -640,6 +641,7 @@ function sortedInsert(list, entry) { } class ELDHistogram extends Histogram { + constructor(i) { super(i); } // eslint-disable-line no-useless-constructor enable() { return this[kHandle].enable(); } disable() { return this[kHandle].disable(); } }