Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1fbd7ac

Browse filesBrowse 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 <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
1 parent d4b41f6 commit 1fbd7ac
Copy full SHA for 1fbd7ac

File tree

Expand file treeCollapse file tree

2 files changed

+23
-9
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+23
-9
lines changed
Open diff view settings
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+16-7Lines changed: 16 additions & 7 deletions
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
/**
Collapse file

‎test/parallel/test-util-inspect.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js
+7-2Lines changed: 7 additions & 2 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.