From b9d55a372a557435ee8947653c15412980cf3e61 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 13 Apr 2018 14:21:45 +0200 Subject: [PATCH] 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. --- lib/util.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/util.js b/lib/util.js index 003d2dfa68a127..d3b08d28d8660a 100644 --- a/lib/util.js +++ b/lib/util.js @@ -445,6 +445,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); @@ -625,11 +630,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');