Skip to content

Commit 77ffd54

Browse files
BridgeARtargos
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 ca47f72 commit 77ffd54

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
@@ -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,
@@ -174,13 +176,20 @@ const meta = [
174176
];
175177

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

186195
/**

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)