util: add fast path to stripVTControlCharacters#61833
Open
privatenumber wants to merge 3 commits intonodejs:mainfrom
Open
util: add fast path to stripVTControlCharacters#61833privatenumber wants to merge 3 commits intonodejs:mainfrom
privatenumber wants to merge 3 commits intonodejs:mainfrom
Conversation
Collaborator
|
Review requested:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #61833 +/- ##
==========================================
+ Coverage 89.72% 89.73% +0.01%
==========================================
Files 675 675
Lines 204797 204801 +4
Branches 39344 39348 +4
==========================================
+ Hits 183752 183788 +36
+ Misses 13324 13282 -42
- Partials 7721 7731 +10
🚀 New features to boost your workflow:
|
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
stripVTControlCharactersalways runs a regex replacement, even when the input contains no ANSI escape codes. This is the common case — most strings passed through are already plain text (e.g. checking/sanitizing user input, processing log lines that are mostly text).Changes
Adds a
StringPrototypeIncludesguard for ESC (\u001B) and CSI (\u009B) before the regex. Since all ANSI escape sequences start with one of these introducers, their absence means no ANSI codes exist and we can return the string immediately — skipping regex execution.Based on the same optimization proposed in chalk/strip-ansi#54.
Also adds a benchmark and test coverage for the fast path, 7-bit ESC sequences, and 8-bit CSI sequences.
Benchmarks
cpu: Apple M2 Max, runtime: node v23.11.0 (arm64-darwin)
Isolated component test showing speedup scaling with string length for non-ANSI input:
ANSI input has negligible overhead from the
includescheck.