Skip to content

Commit 3c5e237

Browse files
committed
doc: edit and simplify util.inspect() docs
PR-URL: #25195 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent ee12607 commit 3c5e237

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

doc/api/util.md

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -432,51 +432,48 @@ changes:
432432

433433
* `object` {any} Any JavaScript primitive or `Object`.
434434
* `options` {Object}
435-
* `showHidden` {boolean} If `true`, the `object`'s non-enumerable symbols and
436-
properties will be included in the formatted result as well as [`WeakMap`][]
437-
and [`WeakSet`][] entries. **Default:** `false`.
435+
* `showHidden` {boolean} If `true`, `object`'s non-enumerable symbols and
436+
properties are included in the formatted result. [`WeakMap`][] and
437+
[`WeakSet`][] entries are also included. **Default:** `false`.
438438
* `depth` {number} Specifies the number of times to recurse while formatting
439-
the `object`. This is useful for inspecting large complicated objects. To
440-
make it recurse up to the maximum call stack size pass `Infinity` or `null`.
439+
`object`. This is useful for inspecting large objects. To recurse up to
440+
the maximum call stack size pass `Infinity` or `null`.
441441
**Default:** `2`.
442-
* `colors` {boolean} If `true`, the output will be styled with ANSI color
443-
codes. Colors are customizable, see [Customizing `util.inspect` colors][].
442+
* `colors` {boolean} If `true`, the output is styled with ANSI color
443+
codes. Colors are customizable. See [Customizing `util.inspect` colors][].
444444
**Default:** `false`.
445-
* `customInspect` {boolean} If `false`, then
446-
`[util.inspect.custom](depth, opts)` functions will not be called.
445+
* `customInspect` {boolean} If `false`,
446+
`[util.inspect.custom](depth, opts)` functions are not invoked.
447447
**Default:** `true`.
448-
* `showProxy` {boolean} If `true`, then objects and functions that are
449-
`Proxy` objects will be introspected to show their `target` and `handler`
450-
objects. **Default:** `false`.
448+
* `showProxy` {boolean} If `true`, `Proxy` inspection includes
449+
the [`target` and `handler`][] objects. **Default:** `false`.
451450
* `maxArrayLength` {integer} Specifies the maximum number of `Array`,
452451
[`TypedArray`][], [`WeakMap`][] and [`WeakSet`][] elements to include when
453452
formatting. Set to `null` or `Infinity` to show all elements. Set to `0` or
454453
negative to show no elements. **Default:** `100`.
455454
* `breakLength` {integer} The length at which an object's keys are split
456455
across multiple lines. Set to `Infinity` to format an object as a single
457456
line. **Default:** `60` for legacy compatibility.
458-
* `compact` {boolean} Setting this to `false` changes the default indentation
459-
to use a line break for each object key instead of lining up multiple
460-
properties in one line. It will also break text that is above the
461-
`breakLength` size into smaller and better readable chunks and indents
462-
objects the same as arrays. Note that no text will be reduced below 16
457+
* `compact` {boolean} Setting this to `false` causes each object key to
458+
be displayed on a new line. It will also add new lines to text that is
459+
longer than `breakLength`. Note that no text will be reduced below 16
463460
characters, no matter the `breakLength` size. For more information, see the
464461
example below. **Default:** `true`.
465462
* `sorted` {boolean|Function} If set to `true` or a function, all properties
466-
of an object and Set and Map entries will be sorted in the returned string.
467-
If set to `true` the [default sort][] is going to be used. If set to a
468-
function, it is used as a [compare function][].
469-
* `getters` {boolean|string} If set to `true`, getters are going to be
470-
inspected as well. If set to `'get'` only getters without setter are going
471-
to be inspected. If set to `'set'` only getters having a corresponding
472-
setter are going to be inspected. This might cause side effects depending on
473-
the getter function. **Default:** `false`.
474-
* Returns: {string} The representation of passed object
463+
of an object, and `Set` and `Map` entries are sorted in the resulting
464+
string. If set to `true` the [default sort][] is used. If set to a function,
465+
it is used as a [compare function][].
466+
* `getters` {boolean|string} If set to `true`, getters are inspected. If set
467+
to `'get'`, only getters without a corresponding setter are inspected. If
468+
set to `'set'`, only getters with a corresponding setter are inspected.
469+
This might cause side effects depending on the getter function.
470+
**Default:** `false`.
471+
* Returns: {string} The representation of `object`.
475472

476473
The `util.inspect()` method returns a string representation of `object` that is
477474
intended for debugging. The output of `util.inspect` may change at any time
478475
and should not be depended upon programmatically. Additional `options` may be
479-
passed that alter certain aspects of the formatted string.
476+
passed that alter the result.
480477
`util.inspect()` will use the constructor's name and/or `@@toStringTag` to make
481478
an identifiable tag for an inspected value.
482479

@@ -504,7 +501,7 @@ const util = require('util');
504501
console.log(util.inspect(util, { showHidden: true, depth: null }));
505502
```
506503

507-
The following example highlights the difference with the `compact` option:
504+
The following example highlights the effect of the `compact` option:
508505

509506
```js
510507
const util = require('util');
@@ -561,13 +558,11 @@ console.log(util.inspect(o, { compact: false, depth: 5, breakLength: 80 }));
561558
// chunks.
562559
```
563560

564-
Using the `showHidden` option allows to inspect [`WeakMap`][] and [`WeakSet`][]
565-
entries. If there are more entries than `maxArrayLength`, there is no guarantee
566-
which entries are displayed. That means retrieving the same [`WeakSet`][]
567-
entries twice might actually result in a different output. Besides this any item
568-
might be collected at any point of time by the garbage collector if there is no
569-
strong reference left to that object. Therefore there is no guarantee to get a
570-
reliable output.
561+
The `showHidden` option allows [`WeakMap`][] and [`WeakSet`][] entries to be
562+
inspected. If there are more entries than `maxArrayLength`, there is no
563+
guarantee which entries are displayed. That means retrieving the same
564+
[`WeakSet`][] entries twice may result in different output. Furthermore, entries
565+
with no remaining strong references may be garbage collected at any time.
571566

572567
```js
573568
const { inspect } = require('util');
@@ -580,8 +575,8 @@ console.log(inspect(weakSet, { showHidden: true }));
580575
// WeakSet { { a: 1 }, { b: 2 } }
581576
```
582577

583-
The `sorted` option makes sure the output is identical, no matter of the
584-
properties insertion order:
578+
The `sorted` option ensures that an object's property insertion order does not
579+
impact the result of `util.inspect()`.
585580

586581
```js
587582
const { inspect } = require('util');
@@ -608,11 +603,9 @@ assert.strict.equal(
608603
);
609604
```
610605

611-
Please note that `util.inspect()` is a synchronous method that is mainly
612-
intended as a debugging tool. Its maximum output length is limited to
613-
approximately 128 MB and input values that result in output bigger than that
614-
will not be inspected fully. Such values can have a significant performance
615-
overhead that can block the event loop for a significant amount of time.
606+
`util.inspect()` is a synchronous method intended for debugging. Its maximum
607+
output length is approximately 128 MB. Inputs that result in longer output will
608+
be truncated.
616609

617610
### Customizing `util.inspect` colors
618611

@@ -2222,6 +2215,7 @@ Deprecated predecessor of `console.log`.
22222215
[`assert.deepStrictEqual()`]: assert.html#assert_assert_deepstrictequal_actual_expected_message
22232216
[`console.error()`]: console.html#console_console_error_data_args
22242217
[`console.log()`]: console.html#console_console_log_data_args
2218+
[`target` and `handler`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy#Terminology
22252219
[`util.format()`]: #util_util_format_format_args
22262220
[`util.inspect()`]: #util_util_inspect_object_options
22272221
[`util.promisify()`]: #util_util_promisify_original

0 commit comments

Comments
 (0)