Skip to content

Commit 2a0ec9c

Browse files
committed
util: improve inspect's customInspect performance
This improves the performance to copy user options that are then passed through to the custom inspect function. The performance improvement depends on the complexity of the custom inspect function. For very basic cases this is 100% faster than before. PR-URL: #30659 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Anto Aravinth <[email protected]>
1 parent 5d2944d commit 2a0ec9c

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

lib/internal/util/inspect.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ const builtInObjects = new Set(
119119
ObjectGetOwnPropertyNames(global).filter((e) => /^([A-Z][a-z]+)+$/.test(e))
120120
);
121121

122+
// These options must stay in sync with `getUserOptions`. So if any option will
123+
// be added or removed, `getUserOptions` must also be updated accordingly.
122124
const inspectDefaultOptions = ObjectSeal({
123125
showHidden: false,
124126
depth: 2,
@@ -176,13 +178,20 @@ const meta = [
176178
];
177179

178180
function getUserOptions(ctx) {
179-
const obj = { stylize: ctx.stylize };
180-
for (const key of ObjectKeys(inspectDefaultOptions)) {
181-
obj[key] = ctx[key];
182-
}
183-
if (ctx.userOptions === undefined)
184-
return obj;
185-
return { ...obj, ...ctx.userOptions };
181+
return {
182+
stylize: ctx.stylize,
183+
showHidden: ctx.showHidden,
184+
depth: ctx.depth,
185+
colors: ctx.colors,
186+
customInspect: ctx.customInspect,
187+
showProxy: ctx.showProxy,
188+
maxArrayLength: ctx.maxArrayLength,
189+
breakLength: ctx.breakLength,
190+
compact: ctx.compact,
191+
sorted: ctx.sorted,
192+
getters: ctx.getters,
193+
...ctx.userOptions
194+
};
186195
}
187196

188197
/**

test/parallel/test-util-inspect.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,10 @@ util.inspect({ hasOwnProperty: null });
884884
assert.strictEqual(opts.budget, undefined);
885885
assert.strictEqual(opts.indentationLvl, undefined);
886886
assert.strictEqual(opts.showHidden, false);
887+
assert.deepStrictEqual(
888+
new Set(Object.keys(util.inspect.defaultOptions).concat(['stylize'])),
889+
new Set(Object.keys(opts))
890+
);
887891
opts.showHidden = true;
888892
return { [util.inspect.custom]: common.mustCall((depth, opts2) => {
889893
assert.deepStrictEqual(clone, opts2);
@@ -910,10 +914,11 @@ util.inspect({ hasOwnProperty: null });
910914
}
911915

912916
{
913-
const subject = { [util.inspect.custom]: common.mustCall((depth) => {
917+
const subject = { [util.inspect.custom]: common.mustCall((depth, opts) => {
914918
assert.strictEqual(depth, null);
919+
assert.strictEqual(opts.compact, true);
915920
}) };
916-
util.inspect(subject, { depth: null });
921+
util.inspect(subject, { depth: null, compact: true });
917922
}
918923

919924
{

0 commit comments

Comments
 (0)