You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
String width calculation is a hot path in terminal rendering — it's called on every line of output in tools like Ink, cli-truncate, wrap-ansi, and log-update. With the growth of AI-powered terminal agents that stream large amounts of text, this is becoming even more performance-sensitive.
I profiled string-width and found two low-hanging optimizations: stripAnsi runs unconditionally even when no ANSI codes are present, and Intl.Segmenter runs on every call even for pure ASCII input where width = length.
ASCII fast path — regex /^[\u0020-\u007E]*$/ detects pure printable ASCII, returns string.length directly. Skips Intl.Segmenter, regex, and EAW lookup entirely. 61x faster for short ASCII, the most common input type.
stripAnsi guard — check string.includes('\x1B') || string.includes('\x9B') before calling stripAnsi. Avoids the overhead of strip-ansi's regex on strings that contain no ANSI escapes. 14x faster for ANSI-free short strings.
Activity
perf: add ASCII fast path and stripAnsi guard
perf: use packed arrays with binary search for lookups
sindresorhus commentedon Feb 18, 2026
#72