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 {
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(); }
}