Skip to content

Commit 7ca1297

Browse files
committed
Use a Set in RemoveParentModulesPlugin for performance
1 parent e8bc9c2 commit 7ca1297

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

‎lib/optimize/RemoveParentModulesPlugin.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,22 @@ function hasModule(chunk, module, checkedChunks) {
88
if(module.isInChunk(chunk)) return [chunk];
99
if(chunk.parents.length === 0) return false;
1010
return allHaveModule(chunk.parents.filter((c) => {
11-
return checkedChunks.indexOf(c) < 0;
11+
return !checkedChunks.has(c);
1212
}), module, checkedChunks);
1313
}
1414

1515
function allHaveModule(someChunks, module, checkedChunks) {
16-
if(!checkedChunks) checkedChunks = [];
17-
var chunks = [];
16+
if(!checkedChunks) checkedChunks = new Set();
17+
var chunks = new Set();
1818
for(var i = 0; i < someChunks.length; i++) {
19-
checkedChunks.push(someChunks[i]);
19+
checkedChunks.add(someChunks[i]);
2020
var subChunks = hasModule(someChunks[i], module, checkedChunks);
2121
if(!subChunks) return false;
2222

2323
for(var index = 0; index < subChunks.length; index++) {
2424
var item = subChunks[index];
2525

26-
if(!chunks.length || chunks.indexOf(item) < 0) {
27-
chunks.push(item);
28-
}
26+
chunks.add(item);
2927
}
3028
}
3129
return chunks;
@@ -48,13 +46,15 @@ class RemoveParentModulesPlugin {
4846

4947
var dId = module.getChunkIdsIdent();
5048
var parentChunksWithModule;
51-
if(dId !== null && (dId in cache)) {
49+
if(dId === null) {
50+
parentChunksWithModule = allHaveModule(chunk.parents, module);
51+
} else if(dId in cache) {
5252
parentChunksWithModule = cache[dId];
5353
} else {
5454
parentChunksWithModule = cache[dId] = allHaveModule(chunk.parents, module);
5555
}
5656
if(parentChunksWithModule) {
57-
module.rewriteChunkInReasons(chunk, parentChunksWithModule);
57+
module.rewriteChunkInReasons(chunk, Array.from(parentChunksWithModule));
5858
chunk.removeModule(module);
5959
}
6060
}

0 commit comments

Comments
 (0)