Skip to content

util: use indexed writes in array inspection - #67911

Open
rozzilla wants to merge 1 commit into
nodejs:mainfrom
rozzilla:util-inspect-array-indexed
Open

rozzilla wants to merge 1 commit into
nodejs:mainfrom
rozzilla:util-inspect-array-indexed

Conversation

@rozzilla

Copy link
Copy Markdown
Contributor

Avoid the cost of primordial push calls for each formatted array element by
using indexed writes in formatArray(). Same approach as #67295 and #67297.

In two local 30-sample runs of benchmark/util/inspect-array.js, dense arrays
improved in all four configurations (+1.0% to +5.6% per run, +2.3% to +4.7%
with both runs pooled). Sparse and mixed arrays take a different path and
showed no significant change.

Avoid the cost of primordial push calls for each formatted array
element.

Assisted-by: Claude Code
Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com>
@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 Nov 30, 2026
@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.58%. Comparing base (91f8170) to head (cea35bc).
⚠️ Report is 24 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #67911   +/-   ##
=======================================
  Coverage   90.58%   90.58%           
=======================================
  Files         802      802           
  Lines      286659   286655    -4     
  Branches    55370    55371    +1     
=======================================
- Hits       259676   259674    -2     
+ Misses      17083    17078    -5     
- Partials     9900     9903    +3     
Files with missing lines Coverage Δ
lib/internal/util/inspect.js 97.03% <100.00%> (ø)

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

@addaleax addaleax left a comment

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.

Doesn't this break with Node.js's goal of having internals be resistant to prototype pollution?

@rozzilla

rozzilla commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Doesn't this break with Node.js's goal of having internals be resistant to prototype pollution?

@addaleax with an index getter and setter defined on Array.prototype, util.inspect([1, 2, 3]) gives the same output and calls the setter the same number of times on main and on this branch.
I tested that, and main and this branch behave the same:

let calls = 0;
Object.defineProperty(Array.prototype, '0', {
  configurable: true,
  set(v) { calls++; },
  get() { return 'polluted'; },
});
const r = require('util').inspect([1, 2, 3]);
console.log(JSON.stringify(r), 'setter calls:', calls);
const a = []; a.push(1);
console.log('plain push on [] hits setter:', calls > 0, a.length);

Both builds return the same output:

"[ polluted, 2, 3 ]" setter calls: 2
plain push on [] hits setter: true 1

The same file already uses indexed writes like this (#67295, #67297).

@addaleax addaleax left a comment

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.

Well, TIL that .push() calls setters 🙂

@addaleax addaleax added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Dec 1, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Dec 1, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

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