Commit 1a36610a716 for nodejs

commit 1a36610a716dd90bfe86a9a22f0e23eb194275d4
Author: Roberto Bianchi <roberto.bianchi@spendesk.com>
Date:   Mon Nov 16 10:15:51 2026 +0100

    util: use indexed writes in Set and Map inspection

    Avoid the cost of primordial push calls for each formatted entry.

    Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com>
    PR-URL: https://github.com/nodejs/node/pull/67295
    Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
    Reviewed-By: Jordan Harband <ljharb@gmail.com>
    Reviewed-By: James M Snell <jasnell@gmail.com>

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;