Complex Iterators are Slow
Thursday, 31 July 2025
Timi, my pure JavaScript B-Tree, achieves best in class iteration speed in part because I replaced Iterators with callbacks.
They might be convenient, but the design of JavaScript Iterators is inherently slow for complex iteration as it prevents your compiler from inlining code.
Inlining is when the call site of a function is replaced with its body to avoid the overhead of a function call. So, this:
function add(a, b) { return a + b; } for (let i = 0; i < 10000; i++) { add(1, 2); }
Becomes this:
for (let i = 0; i < 10000; i++) { 1 + 2; }
Inlining can have a dramatic effect on performance. So it's worth paying attention to. When performance tuning, you can use the --trace-turbo-inlining flag to ask TurboFan (one of V8's optimising compilers) to report when it inlines a function.
$ node --trace-turbo-inlining add.js Considering 0x555db021c350 {0x01e444e159b1
... continue reading