perf: add ASCII fast path and codepoint iteration to avoid Intl.Segmenter#72
Merged
sindresorhus merged 2 commits intosindresorhus:mainfrom Feb 18, 2026
Merged
Conversation
7d0108f to
11330df
Compare
e547caf to
4ba7f25
Compare
Owner
|
I'm happy to accept 1, those are easy wins, but I don't really want 2. It complicates the codebase significantly again, and |
4ba7f25 to
d74bca5
Compare
Contributor
Author
|
Updated:
|
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Intl.SegmenterandstripAnsirun on every call regardless of input. For ASCII strings like"hello world", the segmenter alone costs ~4µs — but width = length.Changes
ASCII fast path
/^[\u0020-\u007E]*$/to detect pure printable ASCII — skip segmenter/regex/EAW entirelystripAnsi guard
stripAnsiwhen no ESC (\x1B) or CSI (\x9B) presentPerformance (Apple M2 Max, Node 25.2.1)
Non-ASCII inputs (CJK, emoji, mixed) are unaffected — they fall through to the existing
Intl.Segmenterpath.All 198 tests pass.
Upstream
Ref: #71