Complex Iterators Are Slow
9 months ago
- #JavaScript
- #Optimization
- #Performance
- Timi, a pure JavaScript B-Tree, achieves best-in-class iteration speed by replacing Iterators with callbacks.
- JavaScript Iterators are inherently slow for complex iteration because they prevent inlining, which can dramatically affect performance.
- Inlining replaces a function call with its body to avoid overhead, improving performance.
- TurboFan, V8's optimizing compiler, can be encouraged to inline functions by keeping them small and simple.
- Complex iteration in Timi involves walks up and down the tree, making the code too complex to be inlined.
- Iterators hide a slow non-inlined call to next() in each loop iteration, which can be avoided with callbacks.
- Callbacks allow the loop to be moved into the iterator, enabling inlining and eliminating function calls inside the loop.
- Benchmarks show that the Iterator API is 4 times slower than the callback API for complex iteration.
- Simple iterators can be performant, but complex iteration may require manual optimization with callbacks.
- Inversion of control with Iterators, generators, and Promises is powerful but should be used carefully in hot code paths.