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.

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