mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
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: https://github.com/nodejs/node/pull/30659 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
This commit is contained in:
1 parent
ca47f72868
commit
77ffd5482d
2 files changed
+23
-9
No files matched your search
@@ -119,6 +119,8 @@ const builtInObjects = new Set(
|
||||
ObjectGetOwnPropertyNames(global).filter((e) => /^([A-Z][a-z]+)+$/.test(e))
|
||||
);
|
||||
|
||||
// These options must stay in sync with `getUserOptions`. So if any option will
|
||||
// be added or removed, `getUserOptions` must also be updated accordingly.
|
||||
const inspectDefaultOptions = ObjectSeal({
|
||||
showHidden: false,
|
||||
depth: 2,
|
||||
@@ -174,13 +176,20 @@ const meta = [
|
||||
];
|
||||
|
||||
function getUserOptions(ctx) {
|
||||
const obj = { stylize: ctx.stylize };
|
||||
for (const key of ObjectKeys(inspectDefaultOptions)) {
|
||||
obj[key] = ctx[key];
|
||||
}
|
||||
if (ctx.userOptions === undefined)
|
||||
return obj;
|
||||
return { ...obj, ...ctx.userOptions };
|
||||
return {
|
||||
stylize: ctx.stylize,
|
||||
showHidden: ctx.showHidden,
|
||||
depth: ctx.depth,
|
||||
colors: ctx.colors,
|
||||
customInspect: ctx.customInspect,
|
||||
showProxy: ctx.showProxy,
|
||||
maxArrayLength: ctx.maxArrayLength,
|
||||
breakLength: ctx.breakLength,
|
||||
compact: ctx.compact,
|
||||
sorted: ctx.sorted,
|
||||
getters: ctx.getters,
|
||||
...ctx.userOptions
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -883,6 +883,10 @@ util.inspect({ hasOwnProperty: null });
|
||||
assert.strictEqual(opts.budget, undefined);
|
||||
assert.strictEqual(opts.indentationLvl, undefined);
|
||||
assert.strictEqual(opts.showHidden, false);
|
||||
assert.deepStrictEqual(
|
||||
new Set(Object.keys(util.inspect.defaultOptions).concat(['stylize'])),
|
||||
new Set(Object.keys(opts))
|
||||
);
|
||||
opts.showHidden = true;
|
||||
return { [util.inspect.custom]: common.mustCall((depth, opts2) => {
|
||||
assert.deepStrictEqual(clone, opts2);
|
||||
@@ -909,10 +913,11 @@ util.inspect({ hasOwnProperty: null });
|
||||
}
|
||||
|
||||
{
|
||||
const subject = { [util.inspect.custom]: common.mustCall((depth) => {
|
||||
const subject = { [util.inspect.custom]: common.mustCall((depth, opts) => {
|
||||
assert.strictEqual(depth, null);
|
||||
assert.strictEqual(opts.compact, true);
|
||||
}) };
|
||||
util.inspect(subject, { depth: null });
|
||||
util.inspect(subject, { depth: null, compact: true });
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user