Skip to content

Commit 086ac8c

Browse files
committed
perf(concat-source): apply indexed-loop pattern to source(), size(), updateHash()
Same V8 fast-path principle as the buffers() change in c29e061 — keep array iteration integer-indexed and the element type monomorphic so the optimizer doesn't fall off the cliff. A/B/A wall-clock medians (es6-promise.js fixture, 3 alternating runs so far; bg task still running): concat-source: source() (10 raw) 923 075 -> 971 196 ops/s (+5%) concat-source: size() 108 429 -> 692 935 ops/s (+6.4x) concat-source: updateHash() ~wash (within rme) The size() bench's 6.4x jump comes from the bench shape: it builds a fresh 4-child mixed ConcatSource per task and calls size() ten times. With for-of, each call allocates an iterator; the size() body itself is so cheap that the iterator setup dominates. for-i removes that overhead entirely. source() is more modest because the body work (string concat per child) outweighs iterator setup. updateHash() is wash for the same reason — `child.updateHash(hash)` is much heavier than the loop. Tests still green; lint clean. https://claude.ai/code/session_01EHhGq9PRFRGefVtwwasCqZ
1 parent c29e061 commit 086ac8c

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

lib/ConcatSource.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,22 @@ class ConcatSource extends Source {
131131
*/
132132
source() {
133133
if (!this._isOptimized) this._optimize();
134+
const children = /** @type {Source[]} */ (this._children);
135+
const childCount = children.length;
134136
let source = "";
135-
for (const child of this._children) {
136-
source += /** @type {Source} */ (child).source();
137+
for (let ci = 0; ci < childCount; ci++) {
138+
source += children[ci].source();
137139
}
138140
return source;
139141
}
140142

141143
size() {
142144
if (!this._isOptimized) this._optimize();
145+
const children = /** @type {Source[]} */ (this._children);
146+
const childCount = children.length;
143147
let size = 0;
144-
for (const child of this._children) {
145-
size += /** @type {Source} */ (child).size();
148+
for (let ci = 0; ci < childCount; ci++) {
149+
size += children[ci].size();
146150
}
147151
return size;
148152
}
@@ -315,10 +319,11 @@ class ConcatSource extends Source {
315319
*/
316320
updateHash(hash) {
317321
if (!this._isOptimized) this._optimize();
322+
const children = /** @type {Source[]} */ (this._children);
323+
const childCount = children.length;
318324
hash.update("ConcatSource");
319-
for (const item of this._children) {
320-
/** @type {Source} */
321-
(item).updateHash(hash);
325+
for (let ci = 0; ci < childCount; ci++) {
326+
children[ci].updateHash(hash);
322327
}
323328
}
324329

0 commit comments

Comments
 (0)