Skip to content

Commit 581fe68

Browse files
mozziejussiKC
andauthored
performance: speed up mergePath child node removal (#2216)
Improved performance on mergePaths plugin. Changed the to-be-deleted node from a list to Set. I ran into performance issues with large files, and some of them were significantly improved by changing the data structure. Does not change any behavior: the list was previously used just for .includes, which is an O(n) call. The .has in set is O(1). Co-authored-by: Jussi Timonen <jussi.timonen.ext@konecranes.com>
1 parent eb4c8b6 commit 581fe68

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

plugins/mergePaths.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export const fn = (root, params) => {
4949
return;
5050
}
5151

52-
/** @type {import('../lib/types.js').XastChild[]} */
53-
const elementsToRemove = [];
52+
/** @type {Set<import('../lib/types.js').XastChild>} */
53+
const elementsToRemove = new Set();
5454
let prevChild = node.children[0];
5555
let prevPathData = null;
5656

@@ -144,7 +144,7 @@ export const fn = (root, params) => {
144144

145145
if (force || !intersects(prevPathData, currentPathData)) {
146146
prevPathData.push(...currentPathData);
147-
elementsToRemove.push(child);
147+
elementsToRemove.add(child);
148148
continue;
149149
}
150150

@@ -161,7 +161,7 @@ export const fn = (root, params) => {
161161
}
162162

163163
node.children = node.children.filter(
164-
(child) => !elementsToRemove.includes(child),
164+
(child) => !elementsToRemove.has(child),
165165
);
166166
},
167167
},

0 commit comments

Comments
 (0)