|
| 1 | +Iterator Includes |
| 2 | +=== |
| 3 | + |
| 4 | +A TC39 proposal to allow the developer to ask whether an iterator yields a given value. Analogue to `Array.prototype.includes`. |
| 5 | + |
| 6 | +**Stage: 0** |
| 7 | +**Specification:** https://tc39.es/proposal-iterator-includes/ |
| 8 | + |
| 9 | +## presentations to committee |
| 10 | + |
| 11 | +- [March 2026](https://docs.google.com/presentation/d/1a-1RQayP-tcJd2VAhFGoiL_dRT8gD8_qrp__qSgC1A4) |
| 12 | + |
| 13 | +## motivation |
| 14 | + |
| 15 | +This proposal has the same motivation as the original |
| 16 | +[`Array.prototype.includes` |
| 17 | +proposal](https://github.com/tc39/proposal-Array.prototype.includes). Just as |
| 18 | +you do with Arrays, at times you need to ask whether an iterator would yield a |
| 19 | +given value. And as with Arrays, you can use `some` with a custom comparator, |
| 20 | +but that's not ideal because it doesn't as directly express your intent, and |
| 21 | +each time you need to do that, you have the option of one of many comparison |
| 22 | +operations, when that choice often doesn't matter. There should be a simple, |
| 23 | +terse, standard way to look for something in the values yielded by an iterator. |
| 24 | + |
| 25 | +## design questions |
| 26 | + |
| 27 | +### comparison operation |
| 28 | + |
| 29 | +There are at least 4 built-in comparison operations that somebody could want to |
| 30 | +use for `includes`: strict equality (`===`), loose equality (`==`), SameValue |
| 31 | +(`Object.is`), and SameValueZero (`Array.prototype.includes`). In a vacuum, we |
| 32 | +could debate the merits of SameValue vs SameValueZero and which is best for |
| 33 | +known popular use cases, but I don't think there's any argument that would be |
| 34 | +stronger than choosing SameValueZero to match `Array.prototype.includes`. |
| 35 | + |
| 36 | +### second parameter (fromIndex) |
| 37 | + |
| 38 | +`Array.prototype.includes` has a second parameter that starts the search from |
| 39 | +the given index instead of the beginning of the Array. This makes sense for the |
| 40 | +Array API because the alternative (slicing first) would first allocate another |
| 41 | +Array and then perform a copy from that index. But Iterators have `drop` which |
| 42 | +is a constant time/space operation, so there's no need to include this |
| 43 | +parameter. That being said, it *could* be included to mirror the Array API, but |
| 44 | +I don't think it's worth it. Additionally, if it was included, it would have to |
| 45 | +reject negative values, which would be an unnecessarily surprising difference. |
| 46 | + |
| 47 | +## chosen solution |
| 48 | + |
| 49 | +A new `Iterator.prototype` method named `includes`. |
| 50 | + |
| 51 | +```js |
| 52 | +function* gen() { yield 1; yield 3; }; |
| 53 | +gen().includes(1); // true |
| 54 | +gen().includes(2); // false |
| 55 | +gen().includes(3); // true |
| 56 | + |
| 57 | +gen().drop(1).includes(1); // false |
| 58 | +gen().drop(1).includes(3); // true |
| 59 | +gen().drop(2).includes(3); // false |
| 60 | +``` |
0 commit comments