From 4686f4f41b0abe4fd2c8836acadf675e072c1ee2 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 0d6eb59992ad54..2b9baa043ab85e 100644 --- a/lib/perf_hooks.js +++ b/lib/perf_hooks.js @@ -3,6 +3,7 @@ const { ArrayIsArray, ArrayPrototypeFilter, + ArrayPrototypeForEach, ArrayPrototypeIncludes, ArrayPrototypeMap, ArrayPrototypePush, @@ -367,13 +368,13 @@ class PerformanceObserver extends AsyncResource { 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) { @@ -400,14 +401,14 @@ class PerformanceObserver extends AsyncResource { 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(); @@ -635,6 +636,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(); } }