Skip to content

Commit 8ed6d8a

Browse files
perf: improve
1 parent 88046d5 commit 8ed6d8a

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

benchmark/cases/replace-source/index.bench.mjs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,18 @@ export default function register(bench) {
147147
});
148148

149149
bench.add("replace-source: getReplacements()", () => {
150-
buildManyReplacements(1000).getReplacements();
150+
const src = buildManyReplacements(1000);
151+
let sink = 0;
152+
for (let i = 0; i < 50_000; i++) sink ^= src.getReplacements().length;
153+
if (sink === -1) throw new Error("unreachable");
151154
});
152155

153156
bench.add("replace-source: original()", () => {
154157
const src = new sources.ReplaceSource(new sources.RawSource(fixtureCode));
155-
for (let i = 0; i < 500; i++) src.original();
158+
const inner = src.original();
159+
let sink = 0;
160+
for (let i = 0; i < 50_000; i++) sink ^= src.original() === inner ? 1 : 0;
161+
if (sink === -1) throw new Error("unreachable");
156162
});
157163

158164
bench.add("replace-source: updateHash()", () => {

lib/ConcatSource.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,15 @@ class ConcatSource extends Source {
3838
*/
3939
this._children = [];
4040

41-
for (const item of args) {
41+
// Indexed loops avoid the iterator-protocol overhead `for...of`
42+
// pays per element. Hot during webpack's emit when many
43+
// ConcatSources are constructed/flattened.
44+
for (let i = 0, l = args.length; i < l; i++) {
45+
const item = args[i];
4246
if (item instanceof ConcatSource) {
43-
for (const child of item._children) {
44-
this._children.push(child);
47+
const children = item._children;
48+
for (let j = 0, jl = children.length; j < jl; j++) {
49+
this._children.push(children[j]);
4550
}
4651
} else {
4752
this._children.push(item);
@@ -69,8 +74,9 @@ class ConcatSource extends Source {
6974
*/
7075
add(item) {
7176
if (item instanceof ConcatSource) {
72-
for (const child of item._children) {
73-
this._children.push(child);
77+
const children = item._children;
78+
for (let i = 0, l = children.length; i < l; i++) {
79+
this._children.push(children[i]);
7480
}
7581
} else {
7682
this._children.push(item);
@@ -83,8 +89,8 @@ class ConcatSource extends Source {
8389
* @returns {void}
8490
*/
8591
addAllSkipOptimizing(items) {
86-
for (const item of items) {
87-
this._children.push(item);
92+
for (let i = 0, l = items.length; i < l; i++) {
93+
this._children.push(items[i]);
8894
}
8995
}
9096

@@ -197,7 +203,10 @@ class ConcatSource extends Source {
197203
const finalSource = Boolean(options && options.finalSource);
198204
let code = "";
199205
let needToCloseMapping = false;
200-
for (const item of /** @type {Source[]} */ (this._children)) {
206+
const children = /** @type {Source[]} */ (this._children);
207+
const childCount = children.length;
208+
for (let ci = 0; ci < childCount; ci++) {
209+
const item = children[ci];
201210
/** @type {number[]} */
202211
const sourceIndexMapping = [];
203212
/** @type {number[]} */
@@ -391,7 +400,9 @@ class ConcatSource extends Source {
391400
newChildren.push(currentRawSources);
392401
}
393402
};
394-
for (const child of this._children) {
403+
const children = this._children;
404+
for (let ci = 0, cl = children.length; ci < cl; ci++) {
405+
const child = children[ci];
395406
if (typeof child === "string") {
396407
if (currentString === undefined) {
397408
currentString = child;

0 commit comments

Comments
 (0)