Skip to content

Commit

Permalink
util: fix inspect performance bug
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
  • Loading branch information
BridgeAR committed Apr 16, 2018
1 parent 5c42578 commit f413f56
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,11 @@ function formatValue(ctx, value, recurseTimes, ln) {
}
}

// 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.indexOf(value) !== -1)
return ctx.stylize('[Circular]', 'special');

let keys;
let symbols = Object.getOwnPropertySymbols(value);

Expand Down Expand Up @@ -640,11 +645,6 @@ function formatValue(ctx, value, recurseTimes, ln) {
}
}

// 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.indexOf(value) !== -1)
return ctx.stylize('[Circular]', 'special');

if (recurseTimes != null) {
if (recurseTimes < 0)
return ctx.stylize(`[${constructor || tag || 'Object'}]`, 'special');
Expand Down

0 comments on commit f413f56

Please sign in to comment.