From c01faf82daf1e2824a234a8e5079b1b3b7821820 Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Tue, 11 Aug 2026 15:24:39 +0200 Subject: [PATCH] util: add maxObjectProperties option to inspect Add a maxObjectProperties option to util.inspect that caps the number of named properties included in formatted output. The limit applies independently from maxArrayLength and defaults to Infinity, preserving existing behavior. Own string and symbol properties are included before user-defined prototype properties. Array, Map, Set, and WeakSet entries governed by maxArrayLength, as well as built-in metadata entries such as [byteLength], [buffer], and [BYTES_PER_ELEMENT], do not consume the property budget. The limit is applied before sorted, and omitted properties are not evaluated (including getters and [util.inspect.custom]). Prototype property collection is deferred so truncated properties are discarded before their values are formatted. Assertion errors pass maxObjectProperties: Infinity explicitly so changes to inspect.defaultOptions do not affect diff output. Signed-off-by: Thomas Watson --- benchmark/util/inspect-object.js | 31 ++ doc/api/util.md | 11 + lib/internal/assert/assertion_error.js | 1 + lib/internal/util/inspect.js | 115 ++++-- test/parallel/test-assert-deep.js | 20 + test/parallel/test-repl-inspect-defaults.js | 6 + test/parallel/test-util-inspect-namespace.js | 8 + test/parallel/test-util-inspect.js | 387 ++++++++++++++++++- 8 files changed, 546 insertions(+), 33 deletions(-) create mode 100644 benchmark/util/inspect-object.js diff --git a/benchmark/util/inspect-object.js b/benchmark/util/inspect-object.js new file mode 100644 index 000000000000..8a6e4dda8b35 --- /dev/null +++ b/benchmark/util/inspect-object.js @@ -0,0 +1,31 @@ +'use strict'; + +const common = require('../common'); +const util = require('util'); + +const bench = common.createBenchmark(main, { + n: [1e3], + len: [1e2, 1e4], + maxObjectProperties: [0, 10, 100, Infinity], + showHidden: [0, 1], +}); + +function main({ n, len, maxObjectProperties, showHidden }) { + const prototype = {}; + const object = { __proto__: prototype }; + for (let i = 0; i < len; i++) { + object[`property${i}`] = { value: i }; + prototype[`prototypeProperty${i}`] = { value: i }; + } + + const options = { + maxObjectProperties, + showHidden: showHidden === 1, + }; + + bench.start(); + for (let i = 0; i < n; i++) { + util.inspect(object, options); + } + bench.end(n); +} diff --git a/doc/api/util.md b/doc/api/util.md index db574ab57461..2ea7adccddd3 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -911,6 +911,9 @@ stream.write('With ES6');