Repository navigation
Conversation
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>
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
addaleax
left a comment
There was a problem hiding this comment.
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 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 1The same file already uses indexed writes like this (#67295, #67297). |
addaleax
left a comment
There was a problem hiding this comment.
Well, TIL that .push() calls setters 🙂
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 arraysimproved 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.