Skip to content

lib: optimize styleText when validateStream is false#61792

Open
RafaelGSS wants to merge 2 commits intonodejs:mainfrom
RafaelGSS:optimize-style-text
Open

lib: optimize styleText when validateStream is false#61792
RafaelGSS wants to merge 2 commits intonodejs:mainfrom
RafaelGSS:optimize-style-text

Conversation

@RafaelGSS
Copy link
Member

This commit optimizes the util.styleText when validateStream is false


➜  node2 git:(27ec4315b1f) ✗ node benchmark/compare.js --old ./node --new ./node-optimize --filter style-text util > style-text.out
[00:00:30|% 100| 1/1 files | 60/60 runs | 24/24 configs]: Done
 %
➜  node2 git:(27ec4315b1f) ✗ node-benchmark-compare style-text.out
                                                                                  confidence improvement accuracy (*)     (**)    (***)
util/style-text.js n=1000 validateStream=0 format='invalid' messageType='boolean'                 3.10 %       ±9.95%  ±13.24%  ±17.23%
util/style-text.js n=1000 validateStream=0 format='invalid' messageType='invalid'                -8.84 %      ±10.73%  ±14.29%  ±18.61%
util/style-text.js n=1000 validateStream=0 format='invalid' messageType='number'                 -8.78 %       ±9.00%  ±12.00%  ±15.67%
util/style-text.js n=1000 validateStream=0 format='invalid' messageType='string'                 -9.09 %       ±9.55%  ±12.71%  ±16.55%
util/style-text.js n=1000 validateStream=0 format='italic' messageType='boolean'                 -4.34 %      ±10.50%  ±13.98%  ±18.20%
util/style-text.js n=1000 validateStream=0 format='italic' messageType='invalid'                 -0.16 %      ±11.98%  ±15.95%  ±20.75%
util/style-text.js n=1000 validateStream=0 format='italic' messageType='number'                  -4.44 %      ±10.75%  ±14.31%  ±18.62%
util/style-text.js n=1000 validateStream=0 format='italic' messageType='string'          ***    572.71 %     ±129.86% ±174.99% ±232.29%
util/style-text.js n=1000 validateStream=0 format='red' messageType='boolean'                     4.81 %      ±11.07%  ±14.73%  ±19.17%
util/style-text.js n=1000 validateStream=0 format='red' messageType='invalid'              *     12.15 %      ±10.44%  ±13.91%  ±18.14%
util/style-text.js n=1000 validateStream=0 format='red' messageType='number'                      4.98 %       ±8.31%  ±11.07%  ±14.44%
util/style-text.js n=1000 validateStream=0 format='red' messageType='string'             ***    602.97 %     ±124.23% ±167.42% ±222.24%
util/style-text.js n=1000 validateStream=1 format='invalid' messageType='boolean'                 1.49 %       ±5.91%   ±7.87%  ±10.24%
util/style-text.js n=1000 validateStream=1 format='invalid' messageType='invalid'                -9.42 %       ±9.62%  ±12.82%  ±16.73%
util/style-text.js n=1000 validateStream=1 format='invalid' messageType='number'                 -4.64 %       ±9.81%  ±13.06%  ±17.02%
util/style-text.js n=1000 validateStream=1 format='invalid' messageType='string'                  9.27 %      ±10.24%  ±13.65%  ±17.83%
util/style-text.js n=1000 validateStream=1 format='italic' messageType='boolean'                  6.04 %       ±9.99%  ±13.30%  ±17.34%
util/style-text.js n=1000 validateStream=1 format='italic' messageType='invalid'                 -3.24 %       ±9.79%  ±13.04%  ±16.98%
util/style-text.js n=1000 validateStream=1 format='italic' messageType='number'                  -1.61 %       ±9.45%  ±12.58%  ±16.38%
util/style-text.js n=1000 validateStream=1 format='italic' messageType='string'                   2.10 %       ±9.03%  ±12.05%  ±15.74%
util/style-text.js n=1000 validateStream=1 format='red' messageType='boolean'                    -0.12 %       ±9.65%  ±12.84%  ±16.71%
util/style-text.js n=1000 validateStream=1 format='red' messageType='invalid'                    -5.44 %      ±11.00%  ±14.64%  ±19.07%
util/style-text.js n=1000 validateStream=1 format='red' messageType='number'                     -1.35 %       ±8.12%  ±10.81%  ±14.08%
util/style-text.js n=1000 validateStream=1 format='red' messageType='string'               *     -7.68 %       ±7.03%   ±9.36%  ±12.20%

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

We are investigating how we can further optimise it without changing the API drastically.

This commit optimizes the util.styleText when validateStream
is false

Co-Authored-By: Bruno Rodrigues <swe@brunocroh.com>
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com>
@RafaelGSS RafaelGSS added the performance Issues and PRs related to the performance of Node.js. label Feb 12, 2026
@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 12, 2026
@RafaelGSS
Copy link
Member Author

RafaelGSS commented Feb 12, 2026

@BridgeAR In our understanding to further optimize this, we would need to encapsulate this API into a Colorize-like API, where we could call .red().bold(). IIRC You have opened a PR to add a similar functionality, but it never landed on main.

Any objections to us pursuing that idea in a separate PR?

@codecov
Copy link

codecov bot commented Feb 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.73%. Comparing base (4a13a62) to head (63d2996).
⚠️ Report is 20 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #61792      +/-   ##
==========================================
- Coverage   89.76%   89.73%   -0.03%     
==========================================
  Files         675      675              
  Lines      204674   204823     +149     
  Branches    39330    39367      +37     
==========================================
+ Hits       183716   183803      +87     
- Misses      13235    13296      +61     
- Partials     7723     7724       +1     
Files with missing lines Coverage Δ
lib/util.js 98.04% <100.00%> (+0.06%) ⬆️

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

@RafaelGSS
Copy link
Member Author

PTAL @brunocroh @nodejs/performance

Copy link
Member

@santigimeno santigimeno left a comment

Choose a reason for hiding this comment

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

LGTM with some questions.

Comment on lines -143 to +200
const formatArray = ArrayIsArray(format) ? format : [format];
const isArray = ArrayIsArray(format);
const formatArray = isArray ? format : [format];
Copy link
Member

Choose a reason for hiding this comment

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

Is this change necessary?

Comment on lines -146 to +206
for (const key of formatArray) {
for (let i = 0; i < formatArray.length; i++) {
Copy link
Member

Choose a reason for hiding this comment

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

Why this change?

Comment on lines -124 to +166
function styleText(format, text, { validateStream = true, stream = process.stdout } = {}) {
function styleText(format, text, options) {
Copy link
Member

Choose a reason for hiding this comment

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

Why getting rid of the default values here?

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. performance Issues and PRs related to the performance of Node.js. 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