I wanted to benchmark iterator iteration vs for loops and the result shocked me – Iterators were 1000 times faster. It feels like I’m not benchmarking it correctly, but if I am, how can you explain such results? btw I also tried benchmarking against for…of loop and the result was the same.
The benchmark on stackblitz (Execute node index.js
in the terminal)
Result:
Array Iteration x 14,662 ops/sec ±2.34% (76 runs sampled)
Iterator Iteration x 48,634,166 ops/sec ±1.85% (78 runs sampled)
Benchmark finished.