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

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

‎lib/perf_hooks.js‎

Lines changed: 7 additions & 5 deletions
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
constobserverCountsGC=observerCounts[NODE_PERFORMANCE_ENTRY_TYPE_GC];
369370
consttypes=this[kTypes];
370-
for(constkeyofObjectKeys(types)){
371+
ArrayPrototypeForEach(ObjectKeys(types),(key)=>{
371372
constitem=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(constentryTypeoffilteredEntryTypes){
404+
ArrayPrototypeForEach(filteredEntryTypes,(entryType)=>{
404405
constlist=getObserversList(entryType);
405-
if(this[kTypes][entryType])continue;
406+
if(this[kTypes][entryType])return;
406407
constitem={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
classELDHistogramextendsHistogram{
639+
constructor(i){super(i);}// eslint-disable-line no-useless-constructor
638640
enable(){returnthis[kHandle].enable();}
639641
disable(){returnthis[kHandle].disable();}
640642
}

0 commit comments

Comments
 (0)