Skip to content

Commit 68f6841

Browse files
BridgeARBethGriggs
authored andcommitted
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 c978396 commit 68f6841

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

lib/internal/util/inspect.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ const builtInObjects = new Set(
121121
ObjectGetOwnPropertyNames(global).filter((e) => /^([A-Z][a-z]+)+$/.test(e))
122122
);
123123

124+
// These options must stay in sync with `getUserOptions`. So if any option will
125+
// be added or removed, `getUserOptions` must also be updated accordingly.
124126
const inspectDefaultOptions = ObjectSeal({
125127
showHidden: false,
126128
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

+7-2
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,10 @@ util.inspect({ hasOwnProperty: null });
883883
assert.strictEqual(opts.budget, undefined);
884884
assert.strictEqual(opts.indentationLvl, undefined);
885885
assert.strictEqual(opts.showHidden, false);
886+
assert.deepStrictEqual(
887+
new Set(Object.keys(util.inspect.defaultOptions).concat(['stylize'])),
888+
new Set(Object.keys(opts))
889+
);
886890
opts.showHidden = true;
887891
return { [util.inspect.custom]: common.mustCall((depth, opts2) => {
888892
assert.deepStrictEqual(clone, opts2);
@@ -909,10 +913,11 @@ util.inspect({ hasOwnProperty: null });
909913
}
910914

911915
{
912-
const subject = { [util.inspect.custom]: common.mustCall((depth) => {
916+
const subject = { [util.inspect.custom]: common.mustCall((depth, opts) => {
913917
assert.strictEqual(depth, null);
918+
assert.strictEqual(opts.compact, true);
914919
}) };
915-
util.inspect(subject, { depth: null });
920+
util.inspect(subject, { depth: null, compact: true });
916921
}
917922

918923
{

0 commit comments

Comments
 (0)