Iterator unique S1
- Stage: Stage 1
- Status: Active
- ECMAScript edition: —
- Synchronized: Aug 28, 2026
- 中文译文 · Source repository
This proposal aims to add a method to Iterator.prototype to produce unique values from any iterator, addressing the difficulty of deduplicating iterators with existing tools. The chosen solution is uniqBy, which takes an optional mapper for uniqueness criteria.
The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.
Iterator Unique
A TC39 proposal to produce an iterator of unique values from any iterator.
Stage: 1
See the January 2024 presentation to committee.
motivation
Removing duplicates from any kind of collection is a common operation. It's not very easy to do for iterators.
For some iterables, you can do something like the following:
This has a few downsides, though:
- Consumes the whole iterator before producing any results.
- Doesn't work for infinite iterators.
- Yields 0 when the underlying iterator yields -0.
- Can't yield both 0 and -0.
- Doesn't work for non-iterable iterators.
A better solution is much harder to write and doesn't work well with chaining, as it requires a bunch of side variables for state.
Worse, when you want to unique by some applied transform, you need to surround the filter with tupling and untupling maps.
chosen solution
Iterator.prototype.uniqBy which takes an optional mapper.
design space
- still no good solution for composite keys, but that's an unsolved problem generally
- mapper? comparator? both? neither?
- separate methods or combined with optional params?
- would mapper be passed an index?
- naming:
distinctis also common