JavaScript's for-of loops are actually fast
15 points by runxiyu
15 points by runxiyu
I mean yeah, but that's just an array-specific optimization.
What's a lot more interesting to get into is what happens if you embrace iteration as a pattern.
The alternative to for of is for in, which is both not useful for any complex types, extremely bad for arrays, and is only fast for property iterations because of very specific optimizations.
I would say that the main alternative to for..of is C-style for loops, since for..in loops over keys as strings.
Still, C-style for loops are just as useless for any complex types as for..in, so your point stands.
Sorry, my comment was super unclear - I meant for generic object traversal for(in) is the alternative. Obviously arrays have many better options :D
I did some benchmarks a while ago https://gist.github.com/easrng/5ab2f2d49309d40462054a4beca1de5c
... at high optimization tiers, in V8, when using arrays. Instantly less true if any of those don't hold (although Safari can also optimize this case pretty well IIRC.)
Incidentally, if any JS engine devs are around, I'm trying to get engines to implement this optimization which would let you skip allocating result objects for native iterators and generators even at the baseline tier. I think it should be pretty easy, someone just needs to do it. I did some work on the spec side to make it more obvious how this would work, even.
I thought maybe this was going to be a GSoC project but I haven't heard anything about it. It would be a great intern project I think.