Skip to content

Commit f4aae23

Browse files
authored
Fix quadratic array copy cost in updateDescendants (#3847)
concat() in the loop created a new array on each iteration, copying all existing elements. Replace with for-of + push() to append in-place, reducing time complexity from O(D²) to O(D).
1 parent c3d7213 commit f4aae23

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

webextensions/common/TreeItem.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,13 +2112,17 @@ export class Tab extends TreeItem {
21122112
}
21132113

21142114
updateDescendants() {
2115-
let descendants = [];
2115+
const descendants = [];
21162116
this.cachedDescendantIds = [];
21172117
for (const child of this.children) {
21182118
descendants.push(child);
2119-
descendants = descendants.concat(child.$TST.descendants);
2119+
for (const descendant of child.$TST.descendants) {
2120+
descendants.push(descendant);
2121+
}
21202122
this.cachedDescendantIds.push(child.id);
2121-
this.cachedDescendantIds = this.cachedDescendantIds.concat(child.$TST.cachedDescendantIds);
2123+
for (const id of child.$TST.cachedDescendantIds) {
2124+
this.cachedDescendantIds.push(id);
2125+
}
21222126
}
21232127
return descendants;
21242128
}

0 commit comments

Comments
 (0)