Also, depending on the VM implementation, it might run into issues if you use the same iterator-function with many different callbacks, if there's a limit to how many different values for the callback argument it specializes; don't know what exactly the limits or effects are, but in some simple testing, both Node/V8 and FF/SpiderMonkey slow down on a callback-based-iterator bench if before the main bench the "iterator" was used with multiple different callbacks (whereas the inverse of a single loop iterating through many different iterator types is probably quite a bit more rare).
myIterator(value => {
total += value;
});
And most people would say this isn't really an iterator in JavaScript any more. This is more like a forEach function. And herein lies the rub: this callback style iteration used to be called internal iterators; the kind of normal iterators shown by the author and also in languages like Rust used to be called external iterators. Eventually people stopped referring to the former style as iterator and I think that's a good terminology change: the internal/external distinction isn't immediately understandable, and it's better to call it a forEach function.I think early Rust even supported the internal iterator style but they abandoned it because IIRC they found external iterations more performant, which is the exact opposite case here.
afdbcreid•6mo ago
dralley•6mo ago