Skip to content

Commit

Permalink
perf_hooks: refactor to avoid unsafe array iteration
Browse files Browse the repository at this point in the history
PR-URL: #36723
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
aduh95 authored and targos committed Jun 11, 2021
1 parent 9338759 commit 4686f4f
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/perf_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
ArrayIsArray,
ArrayPrototypeFilter,
ArrayPrototypeForEach,
ArrayPrototypeIncludes,
ArrayPrototypeMap,
ArrayPrototypePush,
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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(); }
}
Expand Down

0 comments on commit 4686f4f

Please sign in to comment.