Skip to content

Commit 3415a98

Browse files
authored
Merge pull request webpack#7441 from webpack/perf/mutate-reasons
more performant changing of reasons
2 parents de16162 + 271fb7b commit 3415a98

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

lib/optimize/SideEffectsFlagPlugin.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class SideEffectsFlagPlugin {
106106
for (const pair of reexportMaps) {
107107
const module = pair[0];
108108
const map = pair[1];
109+
let newReasons = undefined;
109110
for (let i = 0; i < module.reasons.length; i++) {
110111
const reason = module.reasons[i];
111112
const dep = reason.dependency;
@@ -117,7 +118,6 @@ class SideEffectsFlagPlugin {
117118
if (mapping) {
118119
dep.redirectedModule = mapping.module;
119120
dep.redirectedId = mapping.exportName;
120-
module.reasons.splice(i--, 1);
121121
mapping.module.addReason(
122122
reason.module,
123123
dep,
@@ -126,8 +126,18 @@ class SideEffectsFlagPlugin {
126126
" (skipped side-effect-free modules)"
127127
: "(skipped side-effect-free modules)"
128128
);
129+
// removing the currect reason, by not adding it to the newReasons array
130+
// lazily create the newReasons array
131+
if (newReasons === undefined) {
132+
newReasons = i === 0 ? [] : module.reasons.slice(0, i);
133+
}
134+
continue;
129135
}
130136
}
137+
if (newReasons !== undefined) newReasons.push(reason);
138+
}
139+
if (newReasons !== undefined) {
140+
module.reasons = newReasons;
131141
}
132142
}
133143
}

0 commit comments

Comments
 (0)