From fbabf4bb5c5e88ed84a504f50c6b7ae3ec71bb87 Mon Sep 17 00:00:00 2001 From: Thiago Santos Date: Sun, 16 Aug 2026 20:28:23 -0300 Subject: [PATCH] perf: drop per-element wrapper indirection on offset=0 iterator path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit augmentativeIterateIterable previously wrapped every next() call in a '() => current()' closure even when no skip offset was set. For the common offset=0 case, return finalNext directly as next(), removing one call frame per element for non-array iterables. The skip (offset>0) path keeps its original stable-wrapper semantics: consumers may cache the function reference, so reassigning iterator.next after first use is unsafe — only an internal closure variable is swapped. Measured (interleaved A/B, 100k elements): gen->array addMap -10.8%, filter+map chain -3.8%. Array path and async path are untouched. --- lib/augmentative-iterable.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/augmentative-iterable.js b/lib/augmentative-iterable.js index 1305e4b..5bc1d41 100644 --- a/lib/augmentative-iterable.js +++ b/lib/augmentative-iterable.js @@ -55,17 +55,26 @@ function augmentativeIterateIterable(augmentList, base, offset) { return end; }; - let current = offset > 0 ? () => { - do { - offset--; - if (it.next().done) return end; - } while (offset > 0); - - current = finalNext; - return current(); - } : finalNext; + if (offset > 0) { + let current = () => { + do { + offset--; + if (it.next().done) return end; + } while (offset > 0); + + current = finalNext; + return current(); + }; + + return { + next: () => current(), + error: it.error ? it.error.bind(it) : undefined, + return: it.return ? it.return.bind(it) : undefined, + }; + } + return { - next: () => current(), + next: finalNext, error: it.error ? it.error.bind(it) : undefined, return: it.return ? it.return.bind(it) : undefined, };