Skip to content

util: remove style caches from styleText slow path#63706

Open
araujogui wants to merge 1 commit into
nodejs:mainfrom
araujogui:util-styletext-no-cache
Open

util: remove style caches from styleText slow path#63706
araujogui wants to merge 1 commit into
nodejs:mainfrom
araujogui:util-styletext-no-cache

Conversation

@araujogui
Copy link
Copy Markdown
Member

@araujogui araujogui commented Jun 1, 2026

Remove the caches from the slow path since they don't improve performance there. validation dominates the cost, making the lookup savings negligible

util/style-text.js n=1000 validateStream=0 format='#ff0000' messageType='boolean'                  -0.36 %       ±2.99% ±4.02% ±5.32%
util/style-text.js n=1000 validateStream=0 format='#ff0000' messageType='invalid'                   0.40 %       ±2.49% ±3.34% ±4.39%
util/style-text.js n=1000 validateStream=0 format='#ff0000' messageType='number'            **      2.27 %       ±1.68% ±2.24% ±2.91%
util/style-text.js n=1000 validateStream=0 format='#ff0000' messageType='string'                    0.82 %       ±2.93% ±3.90% ±5.07%
util/style-text.js n=1000 validateStream=0 format='#rotating' messageType='boolean'          *      1.25 %       ±1.23% ±1.64% ±2.14%
util/style-text.js n=1000 validateStream=0 format='#rotating' messageType='invalid'                -0.71 %       ±5.57% ±7.50% ±9.94%
util/style-text.js n=1000 validateStream=0 format='#rotating' messageType='number'           *      2.59 %       ±2.08% ±2.77% ±3.61%
util/style-text.js n=1000 validateStream=0 format='#rotating' messageType='string'                 -2.07 %       ±3.62% ±4.83% ±6.33%
util/style-text.js n=1000 validateStream=0 format='invalid' messageType='boolean'          ***      2.36 %       ±1.28% ±1.71% ±2.24%
util/style-text.js n=1000 validateStream=0 format='invalid' messageType='invalid'           **      1.57 %       ±0.99% ±1.31% ±1.71%
util/style-text.js n=1000 validateStream=0 format='invalid' messageType='number'            **      3.25 %       ±2.35% ±3.13% ±4.08%
util/style-text.js n=1000 validateStream=0 format='invalid' messageType='string'                    2.07 %       ±2.89% ±3.89% ±5.15%
util/style-text.js n=1000 validateStream=0 format='italic' messageType='boolean'             *      1.62 %       ±1.41% ±1.87% ±2.43%
util/style-text.js n=1000 validateStream=0 format='italic' messageType='invalid'           ***      2.00 %       ±0.79% ±1.05% ±1.36%
util/style-text.js n=1000 validateStream=0 format='italic' messageType='number'              *      3.90 %       ±3.08% ±4.15% ±5.50%
util/style-text.js n=1000 validateStream=0 format='italic' messageType='string'                    -1.52 %       ±4.73% ±6.31% ±8.24%
util/style-text.js n=1000 validateStream=0 format='red' messageType='boolean'                       1.68 %       ±1.93% ±2.56% ±3.33%
util/style-text.js n=1000 validateStream=0 format='red' messageType='invalid'               **      2.29 %       ±1.42% ±1.90% ±2.47%
util/style-text.js n=1000 validateStream=0 format='red' messageType='number'                **      2.72 %       ±1.93% ±2.57% ±3.34%
util/style-text.js n=1000 validateStream=0 format='red' messageType='string'                       -0.34 %       ±3.61% ±4.82% ±6.30%
util/style-text.js n=1000 validateStream=1 format='#ff0000' messageType='boolean'                   0.14 %       ±3.35% ±4.51% ±5.96%
util/style-text.js n=1000 validateStream=1 format='#ff0000' messageType='invalid'          ***      1.91 %       ±0.62% ±0.83% ±1.08%
util/style-text.js n=1000 validateStream=1 format='#ff0000' messageType='number'             *      2.08 %       ±1.72% ±2.29% ±2.98%
util/style-text.js n=1000 validateStream=1 format='#ff0000' messageType='string'           ***      2.24 %       ±0.82% ±1.09% ±1.42%
util/style-text.js n=1000 validateStream=1 format='#rotating' messageType='boolean'          *      1.73 %       ±1.31% ±1.75% ±2.28%
util/style-text.js n=1000 validateStream=1 format='#rotating' messageType='invalid'                 0.64 %       ±2.20% ±2.95% ±3.88%
util/style-text.js n=1000 validateStream=1 format='#rotating' messageType='number'         ***      2.98 %       ±1.39% ±1.86% ±2.43%
util/style-text.js n=1000 validateStream=1 format='#rotating' messageType='string'                  0.30 %       ±2.50% ±3.36% ±4.45%
util/style-text.js n=1000 validateStream=1 format='invalid' messageType='boolean'           **      2.27 %       ±1.40% ±1.87% ±2.44%
util/style-text.js n=1000 validateStream=1 format='invalid' messageType='invalid'          ***      1.86 %       ±0.94% ±1.25% ±1.62%
util/style-text.js n=1000 validateStream=1 format='invalid' messageType='number'            **      3.84 %       ±2.81% ±3.75% ±4.91%
util/style-text.js n=1000 validateStream=1 format='invalid' messageType='string'           ***      2.26 %       ±1.28% ±1.71% ±2.24%
util/style-text.js n=1000 validateStream=1 format='italic' messageType='boolean'           ***      2.72 %       ±1.51% ±2.02% ±2.64%
util/style-text.js n=1000 validateStream=1 format='italic' messageType='invalid'           ***      2.01 %       ±0.97% ±1.29% ±1.68%
util/style-text.js n=1000 validateStream=1 format='italic' messageType='number'              *      2.19 %       ±1.76% ±2.34% ±3.06%
util/style-text.js n=1000 validateStream=1 format='italic' messageType='string'                     3.53 %       ±3.78% ±5.10% ±6.75%
util/style-text.js n=1000 validateStream=1 format='red' messageType='boolean'                       1.32 %       ±1.68% ±2.25% ±2.95%
util/style-text.js n=1000 validateStream=1 format='red' messageType='invalid'              ***      1.97 %       ±1.08% ±1.44% ±1.88%
util/style-text.js n=1000 validateStream=1 format='red' messageType='number'                 *      2.37 %       ±1.80% ±2.40% ±3.13%
util/style-text.js n=1000 validateStream=1 format='red' messageType='string'                        2.26 %       ±2.31% ±3.09% ±4.07%

Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 40 comparisons, you can thus
expect the following amount of false-positive results:
  2.00 false positives, when considering a   5% risk acceptance (*, **, ***),
  0.40 false positives, when considering a   1% risk acceptance (**, ***),
  0.04 false positives, when considering a 0.1% risk acceptance (***)

Copilot AI review requested due to automatic review settings June 1, 2026 22:01
@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 Jun 1, 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

Note

Copilot was unable to run its full agentic suite in this review.

This PR refactors styleText to reduce reliance on the prebuilt style cache and to build ANSI sequences directly from inspect.colors, while also simplifying the hex-color styling path.

Changes:

  • Lazily initializes the style cache only in the validateStream=false fast path.
  • Replaces cached style lookups with direct reads from inspect.colors and inline ANSI open/close sequence construction.
  • Reworks hex color handling to compute 24-bit ANSI sequences directly instead of using the hex style cache.

💡 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
Comment thread lib/util.js Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.34%. Comparing base (f30139e) to head (6f4298d).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #63706      +/-   ##
==========================================
- Coverage   90.35%   90.34%   -0.01%     
==========================================
  Files         732      732              
  Lines      236684   236690       +6     
  Branches    44579    44582       +3     
==========================================
- Hits       213855   213845      -10     
- Misses      14544    14557      +13     
- Partials     8285     8288       +3     
Files with missing lines Coverage Δ
lib/util.js 94.60% <100.00%> (+<0.01%) ⬆️

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

@araujogui araujogui force-pushed the util-styletext-no-cache branch from 5dc48c3 to 5980e4f Compare June 2, 2026 00:06
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
@araujogui araujogui force-pushed the util-styletext-no-cache branch from 5980e4f to 6f4298d Compare June 2, 2026 00:08
@araujogui
Copy link
Copy Markdown
Member Author

CC @nodejs/performance

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.

3 participants