perf_hooks: refactor to avoid unsafe array iteration

PR-URL: https://github.com/nodejs/node/pull/36723
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Antoine du Hamel 2020-12-31 12:54:08 +01:00 committed by Node.js GitHub Bot
parent 7c767622bf
commit 5ad67a75a8

View File

@ -3,6 +3,7 @@
const { const {
ArrayIsArray, ArrayIsArray,
ArrayPrototypeFilter, ArrayPrototypeFilter,
ArrayPrototypeForEach,
ArrayPrototypeIncludes, ArrayPrototypeIncludes,
ArrayPrototypeMap, ArrayPrototypeMap,
ArrayPrototypePush, ArrayPrototypePush,
@ -375,13 +376,13 @@ class PerformanceObserver {
disconnect() { disconnect() {
const observerCountsGC = observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC]; const observerCountsGC = observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC];
const types = this[kTypes]; const types = this[kTypes];
for (const key of ObjectKeys(types)) { ArrayPrototypeForEach(ObjectKeys(types), (key) => {
const item = types[key]; const item = types[key];
if (item) { if (item) {
L.remove(item); L.remove(item);
observerCounts[key]--; observerCounts[key]--;
} }
} });
this[kTypes] = {}; this[kTypes] = {};
if (observerCountsGC === 1 && if (observerCountsGC === 1 &&
observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC] === 0) { observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC] === 0) {
@ -408,14 +409,14 @@ class PerformanceObserver {
this[kBuffer][kEntries] = []; this[kBuffer][kEntries] = [];
L.init(this[kBuffer][kEntries]); L.init(this[kBuffer][kEntries]);
this[kBuffering] = Boolean(options.buffered); this[kBuffering] = Boolean(options.buffered);
for (const entryType of filteredEntryTypes) { ArrayPrototypeForEach(filteredEntryTypes, (entryType) => {
const list = getObserversList(entryType); const list = getObserversList(entryType);
if (this[kTypes][entryType]) continue; if (this[kTypes][entryType]) return;
const item = { obs: this }; const item = { obs: this };
this[kTypes][entryType] = item; this[kTypes][entryType] = item;
L.append(list, item); L.append(list, item);
observerCounts[entryType]++; observerCounts[entryType]++;
} });
if (observerCountsGC === 0 && if (observerCountsGC === 0 &&
observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC] === 1) { observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC] === 1) {
installGarbageCollectionTracking(); installGarbageCollectionTracking();
@ -640,6 +641,7 @@ function sortedInsert(list, entry) {
} }
class ELDHistogram extends Histogram { class ELDHistogram extends Histogram {
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
enable() { return this[kHandle].enable(); } enable() { return this[kHandle].enable(); }
disable() { return this[kHandle].disable(); } disable() { return this[kHandle].disable(); }
} }