Skip to content

Commit 4686f4f

Browse files
aduh95targos
authored andcommitted
perf_hooks: refactor to avoid unsafe array iteration
PR-URL: #36723 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 9338759 commit 4686f4f

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/perf_hooks.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
ArrayIsArray,
55
ArrayPrototypeFilter,
6+
ArrayPrototypeForEach,
67
ArrayPrototypeIncludes,
78
ArrayPrototypeMap,
89
ArrayPrototypePush,
@@ -367,13 +368,13 @@ class PerformanceObserver extends AsyncResource {
367368
disconnect() {
368369
const observerCountsGC = observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC];
369370
const types = this[kTypes];
370-
for (const key of ObjectKeys(types)) {
371+
ArrayPrototypeForEach(ObjectKeys(types), (key) => {
371372
const item = types[key];
372373
if (item) {
373374
L.remove(item);
374375
observerCounts[key]--;
375376
}
376-
}
377+
});
377378
this[kTypes] = {};
378379
if (observerCountsGC === 1 &&
379380
observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC] === 0) {
@@ -400,14 +401,14 @@ class PerformanceObserver extends AsyncResource {
400401
this[kBuffer][kEntries] = [];
401402
L.init(this[kBuffer][kEntries]);
402403
this[kBuffering] = Boolean(options.buffered);
403-
for (const entryType of filteredEntryTypes) {
404+
ArrayPrototypeForEach(filteredEntryTypes, (entryType) => {
404405
const list = getObserversList(entryType);
405-
if (this[kTypes][entryType]) continue;
406+
if (this[kTypes][entryType]) return;
406407
const item = { obs: this };
407408
this[kTypes][entryType] = item;
408409
L.append(list, item);
409410
observerCounts[entryType]++;
410-
}
411+
});
411412
if (observerCountsGC === 0 &&
412413
observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC] === 1) {
413414
installGarbageCollectionTracking();
@@ -635,6 +636,7 @@ function sortedInsert(list, entry) {
635636
}
636637

637638
class ELDHistogram extends Histogram {
639+
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
638640
enable() { return this[kHandle].enable(); }
639641
disable() { return this[kHandle].disable(); }
640642
}

0 commit comments

Comments
 (0)