GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — util: improve inspect's customInspect performance · nodejs/node@1fbd7ac

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/util
    • inspect.js
  • test/parallel
    • test-util-inspect.js
Expand file treeCollapse file tree
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)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.