Skip to content

Truncate long Set and Map in util.inspect/console.log/REPL, just like long Array #42933

Description

@lydell

What is the problem this feature will solve?

console.log(), util.inspect() and REPL prints can become unreadable if you happen to have a very large Set or Map as part of the data you’re passing. But if you have a very large Array, the print helpfully cuts off after 100 items (adding something like ... 900 more items at the end).

What is the feature you are proposing to solve the problem?

  1. In 2015, support for Set and Map were added to util.inspect: util: support inspecting Map, Set, and Promise #1471
  2. In 2016, the maxArrayLength option (defaulting to 100) was added to util.inspect: util: truncate inspect array and typed array #6334
  3. In 2018, maxArrayLength was extended to also cover WeakSet and WeakMap: util: improve Weak(Map|Set) support #19259

So maxArrayLength covers Array, TypedArray, WeakMap and WeakSet, but not plain Map and Set.

In the last PR I linked to above, there was even a TODO comment at one point suggesting to rename maxArrayLength to maxEntries: #19259 (comment)

I suggest that maxArrayLength keeps it name (renaming would be another issue), but also applies to Map and Set.

What alternatives have you considered?

I could add something like this to my application:

Set.prototype[util.inspect.custom] = function(depth, opts, inspect) {
  return `Set(${this.size}) ${inspect(Array.from(this))}`;
};

But it feels a bit dirty, and I also seem to lose color that way. I also need to do it in every project, and it’s unclear how to get it into the REPL.

Activity

  1. targos commented on May 2, 2022

    @targos
    Member

    @nodejs/util

  2. added
    utilIssues and PRs related to the built-in util module.
    on May 2, 2022
  3. ljharb commented on May 2, 2022

    @ljharb
    SponsorMember

    Having a max option that applies to entries makes sense, because Record/Tuple are coming - I don't think the name "array" in that option would make sense.

    I think a new maxEntries option that, when provided, takes precedence over maxArrayLength, would be a good semver-minor approach.

  4. moved this to Pending Triage in Node.js feature requestson May 4, 2022
  5. lydell commented on Jul 14, 2022

    @lydell
    Author

    Should this be closed now that #43576 was merged?

  6. github-actions commented on Jan 11, 2023

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

    For more information on how the project manages feature requests, please consult the feature request management document.

  7. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jan 11, 2023
  8. moved this from Pending Triage to Stale in Node.js feature requestson Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions