From 4df70155603f33ac10f609df2c44e84f7ad1404b Mon Sep 17 00:00:00 2001 From: Roberto Bianchi Date: Mon, 16 Nov 2026 10:15:51 +0100 Subject: [PATCH] util: use indexed writes in Set and Map inspection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Avoid the cost of primordial push calls for each formatted entry. Signed-off-by: Roberto Bianchi PR-URL: https://github.com/nodejs/node/pull/67295 Reviewed-By: Gürgün Dayıoğlu Reviewed-By: Jordan Harband Reviewed-By: James M Snell --- benchmark/util/inspect.js | 22 +++++++++++++++++++++- lib/internal/util/inspect.js | 12 +++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/benchmark/util/inspect.js b/benchmark/util/inspect.js index 23beac0039b..43062daff87 100644 --- a/benchmark/util/inspect.js +++ b/benchmark/util/inspect.js @@ -2,6 +2,7 @@ const util = require('util'); const common = require('../common.js'); +const assert = require('node:assert'); const opts = { showHidden: { showHidden: true }, @@ -20,6 +21,8 @@ const bench = common.createBenchmark(main, { 'String_boxed', 'Date', 'Set', + 'Set_large', + 'Map', 'Error', 'Array', 'TypedArray', @@ -30,11 +33,14 @@ const bench = common.createBenchmark(main, { }); function benchmark(n, obj, options) { + let output; bench.start(); for (let i = 0; i < n; i += 1) { - util.inspect(obj, options); + output = util.inspect(obj, options); } bench.end(n); + assert.strictEqual(typeof output, 'string'); + assert.notStrictEqual(output.length, 0); } function main({ method, n, option }) { @@ -85,6 +91,20 @@ function main({ method, n, option }) { obj = new Set([5, 3]); benchmark(n, obj, options); break; + case 'Set_large': + obj = new Set(); + for (let i = 0; i < 50; i++) { + obj.add(i); + } + benchmark(n, obj, options); + break; + case 'Map': + obj = new Map(); + for (let i = 0; i < 50; i++) { + obj.set(i, i); + } + benchmark(n, obj, options); + break; case 'Error': benchmark(n, new Error('error'), options); break; diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 30bfa41b0ce..3128d5e5030 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -2398,11 +2398,11 @@ function formatSet(value, ctx, ignored, recurseTimes) { let i = 0; for (const v of value) { if (i >= maxLength) break; - ArrayPrototypePush(output, formatValue(ctx, v, recurseTimes)); + output[output.length] = formatValue(ctx, v, recurseTimes); i++; } if (remaining > 0) { - ArrayPrototypePush(output, remainingText(remaining)); + output[output.length] = remainingText(remaining); } ctx.indentationLvl -= 2; return output; @@ -2417,14 +2417,12 @@ function formatMap(value, ctx, ignored, recurseTimes) { let i = 0; for (const { 0: k, 1: v } of value) { if (i >= maxLength) break; - ArrayPrototypePush( - output, - `${formatValue(ctx, k, recurseTimes)} => ${formatValue(ctx, v, recurseTimes)}`, - ); + output[output.length] = + `${formatValue(ctx, k, recurseTimes)} => ${formatValue(ctx, v, recurseTimes)}`; i++; } if (remaining > 0) { - ArrayPrototypePush(output, remainingText(remaining)); + output[output.length] = remainingText(remaining); } ctx.indentationLvl -= 2; return output;