Skip to content

Commit 50aa231

Browse files
LiviaMedeirosaduh95
authored andcommitted
util: use Map.prototype.getOrInsert() in circular reference formatting
Signed-off-by: LiviaMedeiros <livia@cirno.name> PR-URL: #67451 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
1 parent c8b430f commit 50aa231

1 file changed

Lines changed: 2 additions & 11 deletions

File tree

‎lib/internal/util/inspect.js‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,17 +1173,8 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
11731173
// Using an array here is actually better for the average case than using
11741174
// a Set. `seen` will only check for the depth and will never grow too large.
11751175
if (ctx.seen.includes(value)) {
1176-
let index = 1;
1177-
if (ctx.circular === undefined) {
1178-
ctx.circular = new SafeMap();
1179-
ctx.circular.set(value, index);
1180-
} else {
1181-
index = ctx.circular.get(value);
1182-
if (index === undefined) {
1183-
index = ctx.circular.size + 1;
1184-
ctx.circular.set(value, index);
1185-
}
1186-
}
1176+
ctx.circular ??= new SafeMap();
1177+
const index = ctx.circular.getOrInsert(value, ctx.circular.size + 1);
11871178
return ctx.stylize(`[Circular *${index}]`, 'special');
11881179
}
11891180

0 commit comments

Comments
 (0)