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 be45469

Browse filesBrowse files
cjihrigtargos
authored andcommitted
doc: edit and simplify util.inspect() docs
PR-URL: #25195 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent bc6f4bc commit be45469
Copy full SHA for be45469

File tree

Expand file treeCollapse file tree

1 file changed

+36
-42
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+36
-42
lines changed
Open diff view settings
Collapse file

‎doc/api/util.md‎

Copy file name to clipboardExpand all lines: doc/api/util.md
+36-42Lines changed: 36 additions & 42 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -419,51 +419,48 @@ changes:
419419

420420
* `object` {any} Any JavaScript primitive or `Object`.
421421
* `options` {Object}
422-
* `showHidden` {boolean} If `true`, the `object`'s non-enumerable symbols and
423-
properties will be included in the formatted result as well as [`WeakMap`][]
424-
and [`WeakSet`][] entries. **Default:** `false`.
422+
* `showHidden` {boolean} If `true`, `object`'s non-enumerable symbols and
423+
properties are included in the formatted result. [`WeakMap`][] and
424+
[`WeakSet`][] entries are also included. **Default:** `false`.
425425
* `depth` {number} Specifies the number of times to recurse while formatting
426-
the `object`. This is useful for inspecting large complicated objects. To
427-
make it recurse up to the maximum call stack size pass `Infinity` or `null`.
426+
`object`. This is useful for inspecting large objects. To recurse up to
427+
the maximum call stack size pass `Infinity` or `null`.
428428
**Default:** `2`.
429-
* `colors` {boolean} If `true`, the output will be styled with ANSI color
430-
codes. Colors are customizable, see [Customizing `util.inspect` colors][].
429+
* `colors` {boolean} If `true`, the output is styled with ANSI color
430+
codes. Colors are customizable. See [Customizing `util.inspect` colors][].
431431
**Default:** `false`.
432-
* `customInspect` {boolean} If `false`, then
433-
`[util.inspect.custom](depth, opts)` functions will not be called.
432+
* `customInspect` {boolean} If `false`,
433+
`[util.inspect.custom](depth, opts)` functions are not invoked.
434434
**Default:** `true`.
435-
* `showProxy` {boolean} If `true`, then objects and functions that are
436-
`Proxy` objects will be introspected to show their `target` and `handler`
437-
objects. **Default:** `false`.
435+
* `showProxy` {boolean} If `true`, `Proxy` inspection includes
436+
the [`target` and `handler`][] objects. **Default:** `false`.
438437
* `maxArrayLength` {integer} Specifies the maximum number of `Array`,
439438
[`TypedArray`][], [`WeakMap`][] and [`WeakSet`][] elements to include when
440439
formatting. Set to `null` or `Infinity` to show all elements. Set to `0` or
441440
negative to show no elements. **Default:** `100`.
442441
* `breakLength` {integer} The length at which an object's keys are split
443442
across multiple lines. Set to `Infinity` to format an object as a single
444443
line. **Default:** `60` for legacy compatibility.
445-
* `compact` {boolean} Setting this to `false` changes the default indentation
446-
to use a line break for each object key instead of lining up multiple
447-
properties in one line. It will also break text that is above the
448-
`breakLength` size into smaller and better readable chunks and indents
449-
objects the same as arrays. Note that no text will be reduced below 16
444+
* `compact` {boolean} Setting this to `false` causes each object key to
445+
be displayed on a new line. It will also add new lines to text that is
446+
longer than `breakLength`. Note that no text will be reduced below 16
450447
characters, no matter the `breakLength` size. For more information, see the
451448
example below. **Default:** `true`.
452449
* `sorted` {boolean|Function} If set to `true` or a function, all properties
453-
of an object and Set and Map entries will be sorted in the returned string.
454-
If set to `true` the [default sort][] is going to be used. If set to a
455-
function, it is used as a [compare function][].
456-
* `getters` {boolean|string} If set to `true`, getters are going to be
457-
inspected as well. If set to `'get'` only getters without setter are going
458-
to be inspected. If set to `'set'` only getters having a corresponding
459-
setter are going to be inspected. This might cause side effects depending on
460-
the getter function. **Default:** `false`.
461-
* Returns: {string} The representation of passed object
450+
of an object, and `Set` and `Map` entries are sorted in the resulting
451+
string. If set to `true` the [default sort][] is used. If set to a function,
452+
it is used as a [compare function][].
453+
* `getters` {boolean|string} If set to `true`, getters are inspected. If set
454+
to `'get'`, only getters without a corresponding setter are inspected. If
455+
set to `'set'`, only getters with a corresponding setter are inspected.
456+
This might cause side effects depending on the getter function.
457+
**Default:** `false`.
458+
* Returns: {string} The representation of `object`.
462459

463460
The `util.inspect()` method returns a string representation of `object` that is
464461
intended for debugging. The output of `util.inspect` may change at any time
465462
and should not be depended upon programmatically. Additional `options` may be
466-
passed that alter certain aspects of the formatted string.
463+
passed that alter the result.
467464
`util.inspect()` will use the constructor's name and/or `@@toStringTag` to make
468465
an identifiable tag for an inspected value.
469466

@@ -491,7 +488,7 @@ const util = require('util');
491488
console.log(util.inspect(util, { showHidden: true, depth: null }));
492489
```
493490

494-
The following example highlights the difference with the `compact` option:
491+
The following example highlights the effect of the `compact` option:
495492

496493
```js
497494
const util = require('util');
@@ -548,13 +545,11 @@ console.log(util.inspect(o, { compact: false, depth: 5, breakLength: 80 }));
548545
// chunks.
549546
```
550547

551-
Using the `showHidden` option allows to inspect [`WeakMap`][] and [`WeakSet`][]
552-
entries. If there are more entries than `maxArrayLength`, there is no guarantee
553-
which entries are displayed. That means retrieving the same [`WeakSet`][]
554-
entries twice might actually result in a different output. Besides this any item
555-
might be collected at any point of time by the garbage collector if there is no
556-
strong reference left to that object. Therefore there is no guarantee to get a
557-
reliable output.
548+
The `showHidden` option allows [`WeakMap`][] and [`WeakSet`][] entries to be
549+
inspected. If there are more entries than `maxArrayLength`, there is no
550+
guarantee which entries are displayed. That means retrieving the same
551+
[`WeakSet`][] entries twice may result in different output. Furthermore, entries
552+
with no remaining strong references may be garbage collected at any time.
558553

559554
```js
560555
const { inspect } = require('util');
@@ -567,8 +562,8 @@ console.log(inspect(weakSet, { showHidden: true }));
567562
// WeakSet { { a: 1 }, { b: 2 } }
568563
```
569564

570-
The `sorted` option makes sure the output is identical, no matter of the
571-
properties insertion order:
565+
The `sorted` option ensures that an object's property insertion order does not
566+
impact the result of `util.inspect()`.
572567

573568
```js
574569
const { inspect } = require('util');
@@ -595,11 +590,9 @@ assert.strict.equal(
595590
);
596591
```
597592

598-
Please note that `util.inspect()` is a synchronous method that is mainly
599-
intended as a debugging tool. Its maximum output length is limited to
600-
approximately 128 MB and input values that result in output bigger than that
601-
will not be inspected fully. Such values can have a significant performance
602-
overhead that can block the event loop for a significant amount of time.
593+
`util.inspect()` is a synchronous method intended for debugging. Its maximum
594+
output length is approximately 128 MB. Inputs that result in longer output will
595+
be truncated.
603596

604597
### Customizing `util.inspect` colors
605598

@@ -2209,6 +2202,7 @@ Deprecated predecessor of `console.log`.
22092202
[`assert.deepStrictEqual()`]: assert.html#assert_assert_deepstrictequal_actual_expected_message
22102203
[`console.error()`]: console.html#console_console_error_data_args
22112204
[`console.log()`]: console.html#console_console_log_data_args
2205+
[`target` and `handler`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy#Terminology
22122206
[`util.format()`]: #util_util_format_format_args
22132207
[`util.inspect()`]: #util_util_inspect_object_options
22142208
[`util.promisify()`]: #util_util_promisify_original

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.