perf(site): use lazy iteration in sliceAtGraphemeBoundary#23671
Merged
perf(site): use lazy iteration in sliceAtGraphemeBoundary#23671
Conversation
Array.from(graphemeSegmenter.segment(text)) materializes the entire text into an array before iterating, even though the loop breaks early at the visible prefix length. During streaming at 60fps, this makes each frame O(full text) instead of O(prefix). Benchmark on 5000-char text with 200-char prefix: 22.6x faster (1.44ms to 0.06ms per call, saving 8.3% of the frame budget). The fallback codepoint path had the same issue with Array.from.
DanielleMaywood
approved these changes
Mar 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix O(full-text) per-frame cost in the streaming text smooth-reveal animation by replacing eager array materialization with lazy iteration.
What changed
sliceAtGraphemeBoundaryinSmoothText.tsusedArray.from(graphemeSegmenter.segment(text))to materialize the entire grapheme segment iterable into an array before iterating with an earlybreak. The fallback codepoint path had the same issue withArray.from(text).During streaming, this function runs on every animation frame (~60fps) for the actively streaming response block. The visible prefix is typically much shorter than the accumulated full text, so the early
breakshould make the function O(prefix), but the eagerArray.fromforces O(full-text) regardless.Replaced both paths with direct
for...ofiteration, which is lazy and exits at thebreak.Benchmark results
Frame budget is measured against 16.7ms (60fps). At 10K characters (a medium-length response), the old code consumed 17.66% of the frame budget on grapheme slicing alone.
Verification
SmoothText.test.tstests pass.lint:compilerreports 181 functions compiled, 0 diagnostics.useSmoothStreamingTexthook still compiles cleanly.