Skip to content

perf(runtime): speed up iterable_array_from for length-bearing values - #66

Merged
cursor[bot] merged 4 commits into
mainfrom
cursor/iterable-array-from-length-fast-path-5b4d
Sep 4, 2026
Merged

cursor[bot] merged 4 commits into
mainfrom
cursor/iterable-array-from-length-fast-path-5b4d

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Second-pass runtime audit after #59. That PR owns the array fast path in iterable_array_from; this change does not touch arrays.

Non-array length-bearing values were still paying the slow path:

  • Plain array-likes ({ length, 0, 1, … } with no iterator) used Array.from(x).slice(index) — two allocations plus a slice.
  • arguments, strings, and typed arrays have a numeric length whose default iterator just walks indexes, but we still allocated an iterator and a { value, done } result per element.

Those now take an indexed copy. Custom iterators that also happen to have length still win (so { length: 2, [Symbol.iterator]: … } is unchanged). Sets, Maps, generators, and arrays stay on the iterator walk.

Skip semantics match the previous path:

  • arguments / strings / typed arrays keep iterator skip (start < index), so negative, fractional, and NaN indexes match.
  • Non-iterable array-likes keep Array.from(x).slice(index) start rules. Sparse holes still become own undefined entries.

Audit notes

Measured on Node v22.14.0, linux x64, isolated perf_hooks processes (warmup 200ms, 400ms × 13 runs, median). Candidates that did not ship:

  • map_iterable prealloc / index-assign — mixed (text +25%, fragment −3.5%), same as the last audit
  • Set/Map Array.from in iterable_array_from — −59% to −63% when isolated (the in-process +191% was noise)
  • Skip Iterator.from for raw iterators — +4.7% isolated (inside noise)
  • array_slice rest-arg removal — 0–3%
  • exclude_prop_from_object via Object.keys — ~11%, plus a closure-correctness risk
  • escape indexOf fast path — +28% clean / −21% dirty
  • create_ref_prop symbol assign — +330% but not compiler-emitted and would make the brand enumerable
  • normalize_spread_props — observation tests lock ownKeys + per-key descriptors; last pass already tuned this in perf(runtime): speed up ordinary spread normalization #42

Target runtimes (React/Preact/Solid/Vue) are re-exports plus error-boundary wrappers. No additional client/JSX/hydration helpers to tune.

Benchmarks

Node v22.14.0, linux x64. Isolated processes. After column is the real @tsrx/runtime export.

Workload Before After Delta
array-like 100, index 0 426,780 ops/s 5,665,321 ops/s +1228%
arguments 10, index 0 2,727,010 ops/s 15,501,706 ops/s +468%
typed array 100, index 0 1,966,341 ops/s 7,644,333 ops/s +289%
string 32, index 0 4,272,467 ops/s 9,232,322 ops/s +116%
CONTROL array 100, index 0 2,340,711 ops/s 2,323,245 ops/s −0.7%
CONTROL Set 100, index 0 2,066,533 ops/s 2,086,234 ops/s +1.0%

Arrays and Sets are intentionally unchanged (iterator path). #59 covers arrays.

Test plan

  • Runtime coverage for array-likes (slice index + holes), arguments/strings/typed arrays (iterator skip), custom iterator+length, Set/array/raw iterator
  • Focused runtime tests: 3 files, 34 passed
  • pnpm test --project tsrx-utils (20 files, 506 tests)
  • Re-ran the same isolated microbenchmark against the real export
  • prettier --check on touched files; pnpm changeset:check
Open in Web Open in Cursor 

Copy indexed elements for non-array array-likes, arguments, strings, and
typed arrays instead of walking Symbol.iterator or allocating through
Array.from().slice(). Arrays stay on the iterator path.

Co-authored-by: Jon Wheeler <[email protected]>
@vercel

vercel Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
tsrx-website Ready Ready Preview Sep 4, 2026 8:42pm UTC
tsrx-website-mcp Ready Ready Preview Sep 4, 2026 8:42pm UTC

Request Review

@cursor

cursor Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

READY FOR REVIEW

@trueadm

trueadm commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Address the PR feedback from cursor please

String.prototype[Symbol.iterator] yields Unicode code points, so the
indexed copy split non-BMP surrogate pairs and changed skip offsets.

Co-authored-by: Jon Wheeler <[email protected]>
@cursor

cursor Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Addressed the Bugbot string surrogate-pair feedback: iterable_array_from keeps strings on the iterator path so non-BMP code points stay intact.

Keep #59 array slice path and this branch's non-array indexed copy.
Strings stay on the iterator path.

Co-authored-by: Jon Wheeler <[email protected]>
Declare arguments-IIFE parameters so checkJs accepts the call, and
narrow length access on the iterable union without changing runtime
behavior.

Co-authored-by: Jon Wheeler <[email protected]>
@cursor
cursor Bot merged commit de31ea4 into main Sep 4, 2026
11 checks passed
@cursor
cursor Bot deleted the cursor/iterable-array-from-length-fast-path-5b4d branch September 4, 2026 20:45
@github-actions github-actions Bot mentioned this pull request Sep 4, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 0f53225. Configure here.

start += 1;
}
return copy_from_offset(array_like, start, length);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indexed copy throws on invalid length

Medium Severity

array_from_index passes the raw length into copy_from_offset, which then does new Array(count). Any value whose iterator is Array.prototype[Symbol.iterator] with a non-integer, negative, NaN, or infinite length now throws RangeError. The previous iterator walk applied ToLength and returned a normal array.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 0f53225. Configure here.

if (!Number.isFinite(start)) {
start = 0;
}
start = Math.trunc(start);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Infinity index copies entire array-like

Low Severity

array_like_from_index maps every non-finite index to 0, including Infinity. Array.from(x).slice(Infinity) returns an empty array, so a non-iterable array-like now returns every element instead of none.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 0f53225. Configure here.

This branch was successfully deployed

2 active deployments
Preview – tsrx-website-mcp — 0f53225b Deployed Sep 4, 2026 by vercel[bot]
Preview – tsrx-website — 0f53225b Deployed Sep 4, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants