Skip to content

util: create hex style cache and fast path#62999

Open
araujogui wants to merge 4 commits intonodejs:mainfrom
araujogui:improve-styletext
Open

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

Conversation

@araujogui
Copy link
Copy Markdown
Member

@araujogui araujogui commented Apr 28, 2026

Improve styleText performance 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 Outdated
Comment thread lib/util.js Outdated
@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 90.04%. Comparing base (bbf51ad) to head (f4f71de).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #62999   +/-   ##
=======================================
  Coverage   90.03%   90.04%           
=======================================
  Files         713      713           
  Lines      224950   225001   +51     
  Branches    42532    42543   +11     
=======================================
+ Hits       202542   202598   +56     
- Misses      14175    14185   +10     
+ Partials     8233     8218   -15     
Files with missing lines Coverage Δ
lib/util.js 94.69% <100.00%> (+0.43%) ⬆️

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

Comment thread lib/util.js Outdated
@araujogui araujogui force-pushed the improve-styletext branch from c879174 to 521eba3 Compare May 8, 2026 22:46
@araujogui araujogui force-pushed the improve-styletext branch from 521eba3 to 5b9ea9d Compare May 9, 2026 00:08
@araujogui
Copy link
Copy Markdown
Member Author

CC @nodejs/performance

Copy link
Copy Markdown
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

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

This cache will grow indefinitely

I don't think we should land like this

Comment thread lib/util.js Outdated
Comment thread lib/util.js Outdated
@araujogui araujogui force-pushed the improve-styletext branch from 49fe099 to f4f71de Compare May 9, 2026 14:34
@araujogui araujogui requested a review from gurgunday May 9, 2026 14:38
Copy link
Copy Markdown
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

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

code lgtm

note: i am unfamiliar with how much this is used

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.

6 participants