Skip to content

util: create hex style cache and fast path#62999

Open
araujogui wants to merge 1 commit intonodejs:mainfrom
araujogui:improve-styletext
Open

util: create hex style cache and fast path#62999
araujogui wants to merge 1 commit intonodejs:mainfrom
araujogui:improve-styletext

Conversation

@araujogui
Copy link
Copy Markdown
Member

Improve styleText by caching and fast-path

Benchmark
util/style-text.js n=1000 validateStream=0 format='#ff0000' messageType='boolean'                -0.71 %       ±1.04%  ±1.38%  ±1.80%
util/style-text.js n=1000 validateStream=0 format='#ff0000' messageType='invalid'                 0.80 %       ±0.86%  ±1.14%  ±1.49%
util/style-text.js n=1000 validateStream=0 format='#ff0000' messageType='number'                 -0.59 %       ±0.93%  ±1.24%  ±1.62%
util/style-text.js n=1000 validateStream=0 format='#ff0000' messageType='string'         ***   1135.88 %      ±31.19% ±42.04% ±55.81%
util/style-text.js n=1000 validateStream=0 format='invalid' messageType='boolean'                -0.09 %       ±1.43%  ±1.90%  ±2.47%
util/style-text.js n=1000 validateStream=0 format='invalid' messageType='invalid'                -0.02 %       ±0.98%  ±1.30%  ±1.69%
util/style-text.js n=1000 validateStream=0 format='invalid' messageType='number'                  0.83 %       ±1.79%  ±2.40%  ±3.15%
util/style-text.js n=1000 validateStream=0 format='invalid' messageType='string'           *     -1.19 %       ±0.97%  ±1.29%  ±1.68%
util/style-text.js n=1000 validateStream=0 format='italic' messageType='boolean'                 -0.24 %       ±1.16%  ±1.55%  ±2.02%
util/style-text.js n=1000 validateStream=0 format='italic' messageType='invalid'                  0.60 %       ±0.92%  ±1.23%  ±1.60%
util/style-text.js n=1000 validateStream=0 format='italic' messageType='number'                   0.81 %       ±1.22%  ±1.63%  ±2.14%
util/style-text.js n=1000 validateStream=0 format='italic' messageType='string'                   1.47 %       ±3.09%  ±4.12%  ±5.36%
util/style-text.js n=1000 validateStream=0 format='red' messageType='boolean'              *      1.17 %       ±1.03%  ±1.37%  ±1.79%
util/style-text.js n=1000 validateStream=0 format='red' messageType='invalid'                     0.73 %       ±0.99%  ±1.32%  ±1.72%
util/style-text.js n=1000 validateStream=0 format='red' messageType='number'                     -0.34 %       ±0.94%  ±1.25%  ±1.63%
util/style-text.js n=1000 validateStream=0 format='red' messageType='string'                      4.92 %       ±6.11%  ±8.17% ±10.72%
util/style-text.js n=1000 validateStream=1 format='#ff0000' messageType='boolean'                -0.50 %       ±1.32%  ±1.76%  ±2.29%
util/style-text.js n=1000 validateStream=1 format='#ff0000' messageType='invalid'                 0.62 %       ±0.89%  ±1.19%  ±1.56%
util/style-text.js n=1000 validateStream=1 format='#ff0000' messageType='number'                 -0.14 %       ±0.79%  ±1.06%  ±1.37%
util/style-text.js n=1000 validateStream=1 format='#ff0000' messageType='string'                 -0.27 %       ±1.23%  ±1.64%  ±2.14%
util/style-text.js n=1000 validateStream=1 format='invalid' messageType='boolean'                -0.31 %       ±0.97%  ±1.29%  ±1.68%
util/style-text.js n=1000 validateStream=1 format='invalid' messageType='invalid'                 0.61 %       ±0.85%  ±1.13%  ±1.48%
util/style-text.js n=1000 validateStream=1 format='invalid' messageType='number'                 -0.05 %       ±0.78%  ±1.04%  ±1.35%
util/style-text.js n=1000 validateStream=1 format='invalid' messageType='string'                  0.69 %       ±1.55%  ±2.07%  ±2.70%
util/style-text.js n=1000 validateStream=1 format='italic' messageType='boolean'                 -0.01 %       ±1.07%  ±1.42%  ±1.85%
util/style-text.js n=1000 validateStream=1 format='italic' messageType='invalid'                 -0.27 %       ±1.09%  ±1.45%  ±1.90%
util/style-text.js n=1000 validateStream=1 format='italic' messageType='number'                   0.04 %       ±1.04%  ±1.38%  ±1.80%
util/style-text.js n=1000 validateStream=1 format='italic' messageType='string'                  -0.41 %       ±0.91%  ±1.21%  ±1.58%
util/style-text.js n=1000 validateStream=1 format='red' messageType='boolean'                     0.56 %       ±1.18%  ±1.57%  ±2.05%
util/style-text.js n=1000 validateStream=1 format='red' messageType='invalid'                    -0.41 %       ±0.94%  ±1.26%  ±1.64%
util/style-text.js n=1000 validateStream=1 format='red' messageType='number'                     -0.88 %       ±0.96%  ±1.28%  ±1.67%
util/style-text.js n=1000 validateStream=1 format='red' messageType='string'                     -0.04 %       ±1.00%  ±1.33%  ±1.73%

Copilot AI review requested due to automatic review settings April 28, 2026 00:33
@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 Apr 28, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves util.styleText() performance for hex colors by introducing a cached hex-style lookup and a fast path for the common case where validateStream: false.

Changes:

  • Add a module-level hexStyleCache and helper (getHexStyle) to memoize computed ANSI sequences for #RGB/#RRGGBB.
  • Reuse a shared close sequence constant for 24-bit foreground colors (\u001b[39m) and integrate hex handling into the validateStream: false fast path.
  • Refactor hex handling in the main formatting loop to use cached sequences instead of re-parsing on every call.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/util.js
Comment on lines +121 to 122
const hexStyleCache = { __proto__: null };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't the maximum possible permutations here 16,781,312 - is the memory size of 17 million key/value pairs a massive concern?

Comment thread lib/util.js
Comment on lines +285 to 292
if (hexStyle === undefined) {
if (RegExpPrototypeExec(hexColorRegExp, key) === null) {
throw new ERR_INVALID_ARG_VALUE('format', key,
'must be a valid hex color (#RGB or #RRGGBB)');
}
hexStyle = getHexStyle(key);
}
if (skipColorize) continue;
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.64%. Comparing base (27c7f4d) to head (3c455e0).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62999      +/-   ##
==========================================
- Coverage   89.67%   89.64%   -0.04%     
==========================================
  Files         707      707              
  Lines      219509   219534      +25     
  Branches    42093    42094       +1     
==========================================
- Hits       196851   196807      -44     
- Misses      14551    14624      +73     
+ Partials     8107     8103       -4     
Files with missing lines Coverage Δ
lib/util.js 94.47% <100.00%> (+0.22%) ⬆️

... and 29 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.

4 participants