From 50aa2316153a80e02408b87d00a005529691d666 Mon Sep 17 00:00:00 2001 From: Livia Medeiros Date: Sun, 15 Nov 2026 19:52:26 +0800 Subject: [PATCH] util: use `Map.prototype.getOrInsert()` in circular reference formatting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: LiviaMedeiros PR-URL: https://github.com/nodejs/node/pull/67451 Reviewed-By: Jordan Harband Reviewed-By: René --- lib/internal/util/inspect.js | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 26cf23bf0fa..30bfa41b0ce 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1173,17 +1173,8 @@ function formatValue(ctx, value, recurseTimes, typedArray) { // Using an array here is actually better for the average case than using // a Set. `seen` will only check for the depth and will never grow too large. if (ctx.seen.includes(value)) { - let index = 1; - if (ctx.circular === undefined) { - ctx.circular = new SafeMap(); - ctx.circular.set(value, index); - } else { - index = ctx.circular.get(value); - if (index === undefined) { - index = ctx.circular.size + 1; - ctx.circular.set(value, index); - } - } + ctx.circular ??= new SafeMap(); + const index = ctx.circular.getOrInsert(value, ctx.circular.size + 1); return ctx.stylize(`[Circular *${index}]`, 'special'); }