Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: change inspect depth default #17907

Closed
wants to merge 7 commits into from

Conversation

@BridgeAR
Copy link
Member

BridgeAR commented Dec 28, 2017

Right now the defaults for util.inspect are often not ideal. This is a first step to improve those. I tried not to break to much and keep the current behavior for the repl and console.

Refs: #12693

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

util, console

@@ -317,7 +317,9 @@ Object.defineProperty(inspect, 'defaultOptions', {
if (options === null || typeof options !== 'object') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
}
Object.assign(inspectDefaultOptions, options);

This comment has been minimized.

@lpinca

lpinca Dec 29, 2017

Member

Any reason for not using _extend?

This comment has been minimized.

@BridgeAR

BridgeAR Dec 29, 2017

Author Member

The type check has to be done again in that case.

This comment has been minimized.

@lpinca

lpinca Dec 29, 2017

Member

I doubt it will add a noticeable overhead but yes it makes sense.

This comment has been minimized.

@TimothyGu

TimothyGu Dec 30, 2017

Member
  1. Do we actually care about the performance of inspection? I thought we didn’t.
  2. Same concern here as @lpinca. This is quite literally reinventing the wheel, and type checks for object are quite fast in modern V8.

This comment has been minimized.

@BridgeAR

BridgeAR Jan 2, 2018

Author Member

Updated the use _extend as it will still be faster than Object.assign

This comment has been minimized.

@BridgeAR

BridgeAR Jan 2, 2018

Author Member

About the performance - just recently I heard that inspect came up in a flamegraph. Seems like people use it a lot. I am aware that setting the default options will not be a big thing but the first implementation (not changing inspect for console.*) used it a lot and that is why I decided to optimize it as well.

@silverwind

This comment has been minimized.

Copy link
Contributor

silverwind commented Dec 29, 2017

Not sure I like the complexity introduced here. Given that this is already flagged as semver-major, why not update it to 5 (I'd leave that value open to discussion, but it's certainly more useful than the current 2) everywhere via new defaults?

@BridgeAR

This comment has been minimized.

Copy link
Member Author

BridgeAR commented Dec 29, 2017

@silverwind I do not see a big difference between 5 and unlimited. Most entries are not that deeply nested and if they are, you likely want to see everything. To prevent huge diffs, the max array entries option is way more efficient as this would otherwise be more difficult to handle.

About the complexity: I am fine with showing everything as a new default for console.* and this would remove the complexity again. But it would be a more significant change and I tried to minimize the breaking change.

For the repl it might be best to stay with the current default though. The reason is that some modules like fs have lots of nested elements and it would be be verbose otherwise.

@BridgeAR

This comment has been minimized.

Copy link
Member Author

BridgeAR commented Dec 29, 2017

@jasnell what do you think about removing the limit for console.* as well? I guess most people would actually profit from that.

@jasnell

This comment has been minimized.

Copy link
Member

jasnell commented Dec 29, 2017

Changing the default for console.* should be ok given that this is semver-major

BridgeAR added some commits Dec 30, 2017

streams: add custom inspect to BufferList
Currently inspecting the BufferList can result a maximum call stack
size error. Adding a individual inspect function prevents this.
util: change util.inspect depth default
The current default is not ideal in most use cases. Therefore it is
changed to showing unlimited depth in case util.inspect is called
directly. The default is kept as before for console.log and similar.

Using console.dir will now show a depth of up to five and
console.assert / console.trace will show a unlimited depth.
util: improve setting default options
Object.assign is currently very slow. Using Object.keys is much
faster in v8 6.3.

@BridgeAR BridgeAR force-pushed the BridgeAR:change-inspect-defaults branch from 2bb38d6 to 2721767 Dec 30, 2017

@BridgeAR

This comment has been minimized.

Copy link
Member Author

BridgeAR commented Dec 30, 2017

I changed the code to default to unlimited for the console functions as well.
To make that work I had to add a custom inspect function to the BufferList.

CITGM https://ci.nodejs.org/view/Node.js-citgm/job/citgm-smoker/1179/
CI https://ci.nodejs.org/job/node-test-pull-request/12350/

@@ -73,4 +74,8 @@ module.exports = class BufferList {
}
return ret;
}

[customInspectSymbol]() {
return `${this.constructor.name} { length: ${this.length} }`;

This comment has been minimized.

@TimothyGu

TimothyGu Dec 30, 2017

Member

You could return { __proto__: Object.getPrototypeOf(this), length: this.length } which would also give coloring etc. for free.

This comment has been minimized.

@BridgeAR

BridgeAR Jan 2, 2018

Author Member

Using __proto__ will actually result in a `call stack size exceeded error. I decided to call inspect directly instead to highlight the color in case it is needed.

@@ -317,7 +317,9 @@ Object.defineProperty(inspect, 'defaultOptions', {
if (options === null || typeof options !== 'object') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
}
Object.assign(inspectDefaultOptions, options);

This comment has been minimized.

@TimothyGu

TimothyGu Dec 30, 2017

Member
  1. Do we actually care about the performance of inspection? I thought we didn’t.
  2. Same concern here as @lpinca. This is quite literally reinventing the wheel, and type checks for object are quite fast in modern V8.
@BridgeAR

This comment has been minimized.

Copy link
Member Author

BridgeAR commented Jan 2, 2018

@BridgeAR BridgeAR added author ready and removed author ready labels Jan 2, 2018

@BridgeAR BridgeAR requested a review from nodejs/tsc Jan 2, 2018

@mcollina
Copy link
Member

mcollina left a comment

LGTM

@targos
Copy link
Member

targos left a comment

Can you please change the documentation of util.inspect (and add an entry to its history)?

@@ -73,4 +75,9 @@ module.exports = class BufferList {
}
return ret;
}

[customInspectSymbol]() {

This comment has been minimized.

@addaleax

addaleax Jan 2, 2018

Member

I’d just use util.inspect.custom here, it saves people looking at this code one round-trip to another file

This comment has been minimized.

@BridgeAR

BridgeAR Jan 3, 2018

Author Member

Done

BridgeAR added some commits Jan 3, 2018

util: change %o depth default
Since the default for depth is changed to `Infinity` it is logical
to change the %o default to the same as well.

Using %o with `util.format` will now always print the whole object.
@BridgeAR

This comment has been minimized.

Copy link
Member Author

BridgeAR commented Jan 3, 2018

I updated the documentation and added the history entry. I also changed the %o default of util.format to also use Infinity as default.

@targos

targos approved these changes Jan 7, 2018

* `depth` {number} Specifies the number visible nested Objects in an `object`.
This is useful to minimize the inspection output for large complicated
objects. To make it recurse indefinitely pass `null` or `Infinity`. Defaults
to `null`.

This comment has been minimized.

@targos

targos Jan 7, 2018

Member

suggestion: Infinity instead of null to match the changelog?

shisama added a commit to shisama/node that referenced this pull request May 13, 2018

Revert "util: change %o depth default"
This reverts commit 8f15309.

PR-URL: nodejs#20017
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request May 13, 2018

Revert "util: change util.inspect depth default"
This reverts commit b994b8e.

This caused regressions in ecosystem code. While the change originally
was semver-major and could be postponed until after Node.js 10,
I think reverting it is a good choice at this point.

Also, I personally do not think defaulting to a shallow inspect
is a bad thing at all – quite the opposite: It makes `util.inspect()`
give an overview of an object, rather than providing a full
display of its contents. Changing the `depth` default to infinity
fundamentally changed the role that `util.inspect()` plays,
and makes output much more verbose and thus at times unusable
for `console.log()`-style debugging.

PR-URL: nodejs#20017
Fixes: nodejs#19405
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request May 30, 2018

Revert "util: change %o depth default"
This reverts commit 8f15309.

PR-URL: nodejs#20017
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request May 30, 2018

Revert "util: change util.inspect depth default"
This reverts commit b994b8e.

This caused regressions in ecosystem code. While the change originally
was semver-major and could be postponed until after Node.js 10,
I think reverting it is a good choice at this point.

Also, I personally do not think defaulting to a shallow inspect
is a bad thing at all – quite the opposite: It makes `util.inspect()`
give an overview of an object, rather than providing a full
display of its contents. Changing the `depth` default to infinity
fundamentally changed the role that `util.inspect()` plays,
and makes output much more verbose and thus at times unusable
for `console.log()`-style debugging.

PR-URL: nodejs#20017
Fixes: nodejs#19405
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request May 30, 2018

Revert "util: change %o depth default"
This reverts commit 8f15309.

PR-URL: nodejs#20017
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request May 30, 2018

Revert "util: change util.inspect depth default"
This reverts commit b994b8e.

This caused regressions in ecosystem code. While the change originally
was semver-major and could be postponed until after Node.js 10,
I think reverting it is a good choice at this point.

Also, I personally do not think defaulting to a shallow inspect
is a bad thing at all – quite the opposite: It makes `util.inspect()`
give an overview of an object, rather than providing a full
display of its contents. Changing the `depth` default to infinity
fundamentally changed the role that `util.inspect()` plays,
and makes output much more verbose and thus at times unusable
for `console.log()`-style debugging.

PR-URL: nodejs#20017
Fixes: nodejs#19405
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request Jun 5, 2018

Revert "util: change %o depth default"
This reverts commit 8f15309.

PR-URL: nodejs#20017
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request Jun 5, 2018

Revert "util: change util.inspect depth default"
This reverts commit b994b8e.

This caused regressions in ecosystem code. While the change originally
was semver-major and could be postponed until after Node.js 10,
I think reverting it is a good choice at this point.

Also, I personally do not think defaulting to a shallow inspect
is a bad thing at all – quite the opposite: It makes `util.inspect()`
give an overview of an object, rather than providing a full
display of its contents. Changing the `depth` default to infinity
fundamentally changed the role that `util.inspect()` plays,
and makes output much more verbose and thus at times unusable
for `console.log()`-style debugging.

PR-URL: nodejs#20017
Fixes: nodejs#19405
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request Jun 9, 2018

Revert "util: change %o depth default"
This reverts commit 8f15309.

PR-URL: nodejs#20017
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request Jun 9, 2018

Revert "util: change util.inspect depth default"
This reverts commit b994b8e.

This caused regressions in ecosystem code. While the change originally
was semver-major and could be postponed until after Node.js 10,
I think reverting it is a good choice at this point.

Also, I personally do not think defaulting to a shallow inspect
is a bad thing at all – quite the opposite: It makes `util.inspect()`
give an overview of an object, rather than providing a full
display of its contents. Changing the `depth` default to infinity
fundamentally changed the role that `util.inspect()` plays,
and makes output much more verbose and thus at times unusable
for `console.log()`-style debugging.

PR-URL: nodejs#20017
Fixes: nodejs#19405
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request Jun 11, 2018

Revert "util: change %o depth default"
This reverts commit 8f15309.

PR-URL: nodejs#20017
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request Jun 11, 2018

Revert "util: change util.inspect depth default"
This reverts commit b994b8e.

This caused regressions in ecosystem code. While the change originally
was semver-major and could be postponed until after Node.js 10,
I think reverting it is a good choice at this point.

Also, I personally do not think defaulting to a shallow inspect
is a bad thing at all – quite the opposite: It makes `util.inspect()`
give an overview of an object, rather than providing a full
display of its contents. Changing the `depth` default to infinity
fundamentally changed the role that `util.inspect()` plays,
and makes output much more verbose and thus at times unusable
for `console.log()`-style debugging.

PR-URL: nodejs#20017
Fixes: nodejs#19405
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request Jun 12, 2018

Revert "util: change %o depth default"
This reverts commit 8f15309.

PR-URL: nodejs#20017
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request Jun 12, 2018

Revert "util: change util.inspect depth default"
This reverts commit b994b8e.

This caused regressions in ecosystem code. While the change originally
was semver-major and could be postponed until after Node.js 10,
I think reverting it is a good choice at this point.

Also, I personally do not think defaulting to a shallow inspect
is a bad thing at all – quite the opposite: It makes `util.inspect()`
give an overview of an object, rather than providing a full
display of its contents. Changing the `depth` default to infinity
fundamentally changed the role that `util.inspect()` plays,
and makes output much more verbose and thus at times unusable
for `console.log()`-style debugging.

PR-URL: nodejs#20017
Fixes: nodejs#19405
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

Revert "util: change %o depth default"
This reverts commit 8f15309.

PR-URL: nodejs#20017
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

Revert "util: change util.inspect depth default"
This reverts commit b994b8e.

This caused regressions in ecosystem code. While the change originally
was semver-major and could be postponed until after Node.js 10,
I think reverting it is a good choice at this point.

Also, I personally do not think defaulting to a shallow inspect
is a bad thing at all – quite the opposite: It makes `util.inspect()`
give an overview of an object, rather than providing a full
display of its contents. Changing the `depth` default to infinity
fundamentally changed the role that `util.inspect()` plays,
and makes output much more verbose and thus at times unusable
for `console.log()`-style debugging.

PR-URL: nodejs#20017
Fixes: nodejs#19405
Refs: nodejs#17907
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>

BridgeAR added a commit to BridgeAR/node that referenced this pull request Sep 13, 2018

util: change util.inspect depth default
The current default is not ideal in most use cases. Therefore it is
changed to inspect objects to a maximum depth of 20 in case
util.inspect is called with it's defaults. The default is kept at 2
when using console.log() and similar in the repl.

PR-URL: nodejs#17907
Refs: nodejs#12693
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>

BridgeAR added a commit to BridgeAR/node that referenced this pull request Sep 13, 2018

util: change %o depth default
Since the default for depth is changed to `20` it is logical
to change the %o default as well. It will now always use the
default depth.

PR-URL: nodejs#17907
Refs: nodejs#12693
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>

BridgeAR added a commit to BridgeAR/node that referenced this pull request Sep 13, 2018

util: change util.inspect depth default
The current default is not ideal in most use cases. Therefore it is
changed to inspect objects to a maximum depth of 20 in case
util.inspect is called with it's defaults. The default is kept at 2
when using console.log() and similar in the repl.

PR-URL: nodejs#17907
Refs: nodejs#12693

BridgeAR added a commit to BridgeAR/node that referenced this pull request Sep 13, 2018

util: change %o depth default
Since the default for depth is changed to `20` it is logical
to change the %o default as well. It will now always use the
default depth.

PR-URL: nodejs#17907
Refs: nodejs#12693

BridgeAR added a commit to BridgeAR/node that referenced this pull request Sep 18, 2018

util: change util.inspect depth default
The current default is not ideal in most use cases. Therefore it is
changed to inspect objects to a maximum depth of 20 in case
util.inspect is called with it's defaults. The default is kept at 2
when using console.log() and similar in the repl.

PR-URL: nodejs#17907
Refs: nodejs#12693

BridgeAR added a commit to BridgeAR/node that referenced this pull request Sep 18, 2018

util: change %o depth default
Since the default for depth is changed to `20` it is logical
to change the %o default as well. It will now always use the
default depth.

PR-URL: nodejs#17907
Refs: nodejs#12693

BridgeAR added a commit to BridgeAR/node that referenced this pull request Sep 19, 2018

util: change util.inspect depth default
The current default is not ideal in most use cases. Therefore it is
changed to inspect objects to a maximum depth of 20 in case
util.inspect is called with it's defaults. The default is kept at 2
when using console.log() and similar in the repl.

PR-URL: nodejs#17907
Refs: nodejs#12693

BridgeAR added a commit to BridgeAR/node that referenced this pull request Sep 19, 2018

util: change %o depth default
Since the default for depth is changed to `20` it is logical
to change the %o default as well. It will now always use the
default depth.

PR-URL: nodejs#17907
Refs: nodejs#12693

BridgeAR added a commit to BridgeAR/node that referenced this pull request Sep 24, 2018

util: change util.inspect depth default
The current default is not ideal in most use cases. Therefore it is
changed to inspect objects to a maximum depth of 20 in case
util.inspect is called with it's defaults. The default is kept at 2
when using console.log() and similar in the repl.

PR-URL: nodejs#17907
Refs: nodejs#12693

BridgeAR added a commit to BridgeAR/node that referenced this pull request Sep 24, 2018

util: change %o depth default
Since the default for depth is changed to `20` it is logical
to change the %o default as well. It will now always use the
default depth.

PR-URL: nodejs#17907
Refs: nodejs#12693

BridgeAR added a commit to BridgeAR/node that referenced this pull request Oct 2, 2018

util: change util.inspect depth default
The current default is not ideal in most use cases. Therefore it is
changed to inspect objects to a maximum depth of 20 in case
util.inspect is called with it's defaults. The default is kept at 2
when using console.log() and similar in the repl.

PR-URL: nodejs#17907
Refs: nodejs#12693

PR-URL: nodejs#22846
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>

BridgeAR added a commit to BridgeAR/node that referenced this pull request Oct 2, 2018

util: change %o depth default
Since the default for depth is changed to `20` it is logical
to change the %o default as well. It will now always use the
default depth.

PR-URL: nodejs#17907
Refs: nodejs#12693

PR-URL: nodejs#22846
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>

@BridgeAR BridgeAR deleted the BridgeAR:change-inspect-defaults branch Apr 1, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.