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 danielleadams committed Jan 12, 2021
1 parent 5bc130b commit a258bc9
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 @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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(); }
}
Expand Down

0 comments on commit a258bc9

Please sign in to comment.