top | item 47194230

(no title)

conartist6 | 1 day ago

The engine can optimize all those allocations out of existence so they never happen at all, so it's not a problem we'll be stuck with forever, just a temporary inconvenience.

If a generator is yielding values it doesn't expose step objects to its inner code. If a `for of` loop is consuming yielded values from that generator, step objects are not exposed directly to the looping code either.

So now when you have a `for of` loop consuming a generator you have step objects which only the engine ever can see, and so the engine is free to optimize the allocations away.

The simplest way the engine could do it is just to reuse the same step object over and over again, mutating step.value between each invocation of next().

discuss

order

burntcaramel|1 day ago

Can optimize in theory, or are you saying this is what JavaScript engines do today?

conartist6|17 hours ago

I don't think any engines today do this. It has not been worth spending the time optimizing because so few people are using iterators heavily.