v154 · iterator includes
Where it stops
The iterator below counts every value it is asked for. Put the target near the start and the difference is stark; put it at the end and the two approaches converge. That curve is the feature.
Search the same sequence two ways
| approach | found? | values pulled | time |
|---|---|---|---|
| Not run yet. | |||
Work done, to scale
Both answers are the same — this is not about correctness. It is about how much of the sequence had to exist for the answer to be produced, which matters when each value costs a parse, a decode, or a network round trip.
code path
function* counted(n, target, tally) {
for (let i = 0; i < n; i++) { tally.pulls++; yield i === target ? "hit" : i; }
}
// Stops at the match.
counted(n, at, a).includes("hit");
// Consumes everything, then searches what it built.
[...counted(n, at, b)].includes("hit");