Skip to content

Commit a167b6a

Browse files
BridgeARjasnell
authored andcommitted
util: fix inspect performance bug
In case an object contained a circular reference `Object.keys` was called even though it was not necessary at all. This caused a significant overhead for objects that contained a lot of such entries. PR-URL: #20007 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent eca95a9 commit a167b6a

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

lib/util.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ function formatValue(ctx, value, recurseTimes, ln) {
456456
}
457457
}
458458

459+
// Using an array here is actually better for the average case than using
460+
// a Set. `seen` will only check for the depth and will never grow too large.
461+
if (ctx.seen.indexOf(value) !== -1)
462+
return ctx.stylize('[Circular]', 'special');
463+
459464
let keys;
460465
let symbols = Object.getOwnPropertySymbols(value);
461466

@@ -640,11 +645,6 @@ function formatValue(ctx, value, recurseTimes, ln) {
640645
}
641646
}
642647

643-
// Using an array here is actually better for the average case than using
644-
// a Set. `seen` will only check for the depth and will never grow too large.
645-
if (ctx.seen.indexOf(value) !== -1)
646-
return ctx.stylize('[Circular]', 'special');
647-
648648
if (recurseTimes != null) {
649649
if (recurseTimes < 0)
650650
return ctx.stylize(`[${constructor || tag || 'Object'}]`, 'special');

0 commit comments

Comments
 (0)