Skip to content

util: add fast path to stripVTControlCharacters#61833

Open
privatenumber wants to merge 3 commits intonodejs:mainfrom
privatenumber:strip-vt-fast-path
Open

util: add fast path to stripVTControlCharacters#61833
privatenumber wants to merge 3 commits intonodejs:mainfrom
privatenumber:strip-vt-fast-path

Conversation

@privatenumber
Copy link
Contributor

@privatenumber privatenumber commented Feb 15, 2026

Problem

stripVTControlCharacters always 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 StringPrototypeIncludes guard 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:

Length Before After Speedup
50 chars 40 ns 20 ns 2.0x
200 chars 124 ns 35 ns 3.6x
1,000 chars 414 ns 67 ns 6.2x
3,000 chars 1,337 ns 201 ns 6.7x
10,000 chars 4,683 ns 739 ns 6.3x
50,000 chars 23,949 ns 2,755 ns 8.7x
100,000 chars 49,957 ns 5,719 ns 8.7x

ANSI input has negligible overhead from the includes check.

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module. labels Feb 15, 2026
@privatenumber privatenumber marked this pull request as ready for review February 15, 2026 03:15
@codecov
Copy link

codecov bot commented Feb 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.73%. Comparing base (9cc7fcc) to head (003511d).

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     
Files with missing lines Coverage Δ
lib/internal/util/inspect.js 99.67% <100.00%> (+<0.01%) ⬆️

... and 33 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants