Avoid iterators/generators to make Javascript fast
Tags: javascript, performance, webdev, ux
Published: 2024-05-17 12:09:43

If we look at the iterator protocol we'd notice it comes at a cost of creating `{value, done}` objects, accessing their props, and calls of `next()` (a function call is generally is an expensive operation). 

Given all the hurdles we have a quite inefficient tool to iterate our data. Moreover when we implement an iterator with a generator we suddenly notice that performance got even worse. I guess that's because of the cost of keeping a generator's scope and its state between calls.

Don't get me wrong, iterators and generators are good abstractions, `for await` is exceptionally amazing when dealing with async data and abstracting backend API calls, so generally used in business logic they bring benefits but when we descend into the realm of transforming data, generic data handling functions and algorithms we should care to avoid them when possible.

Here's a benchmark of several ways to iterate an array and you see  the iterator and generator fail considerably. I'm not the only one complaining, I've seen several comments that generators' speed keeps people from using them widely.
```js
class Iter {
  *gen(arr) {
    for (let i = 0; i < arr.length; i++) {
      yield arr[i];
    }
  }
  iter(arr) {
    let i = 0;
    const out = { value: null, done: false };
    const iter = {
      [Symbol.iterator]: () => iter,
      next: () => {
        if (i === arr.length) out.done = true, out.value = null;
        else out.value = arr[i++];
        return out;
      }
    };
    return iter;
  }
}

const $chunk = () => Array.from({length: 10}, (_, i) => i);
const $input = [], arr = $input;
const iter = new Iter;

// @benchmark generator
for(const e of iter.gen(arr));

// @benchmark iterator
for(const e of iter.iter(arr));

// @benchmark for..of
for(const e of arr);

// @benchmark for
for(let i = 0; i < arr.length; i++) arr[i];

// @benchmark forEach
arr.forEach(e => {});

` Chrome/125
-----------------------------------------------------------------------------------------
>                 n=10        |       n=100       |      n=1000       |      n=10000     
for           1.17x x100m 336 |   1.20x  x10m 354 |   1.01x   x1m 235 | ■ 1.00x x100k 218
for..of       1.19x x100m 340 |   1.21x  x10m 355 | ■ 1.00x   x1m 232 |   1.00x x100k 219
forEach     ■ 1.00x x100m 286 | ■ 1.00x  x10m 294 |   1.00x   x1m 233 |   4.54x  x10k  99
iterator     64.34x   x1m 184 |  13.71x   x1m 403 |  10.86x x100k 252 |  10.96x  x10k 239
generator    43.01x   x1m 123 |  36.05x x100k 106 |  45.69x  x10k 106 |  43.58x   x1k  95
----------------------------------------------------------------------------------------- `
` Firefox/126
-----------------------------------------------------------------------------------------
>                 n=10        |       n=100       |      n=1000       |      n=10000     
for         ■ 1.00x x100m 512 | ■ 1.00x  x10m 505 | ■ 1.00x   x1m 453 | ■ 1.00x x100k 449
forEach       1.62x x100m 828 |   1.48x  x10m 746 |   4.26x x100k 193 |   6.57x  x10k 295
for..of      11.07x  x10m 567 |   9.35x   x1m 472 |   9.98x x100k 452 |  10.11x  x10k 454
iterator     23.44x   x1m 120 |  10.93x   x1m 552 |  11.24x x100k 509 |  10.71x  x10k 481
generator    82.62x   x1m 423 |  64.75x x100k 327 |  66.67x  x10k 302 |  68.37x   x1k 307
----------------------------------------------------------------------------------------- `
```
[Open in the playground](https://silentmantra.github.io/benchmark#H4sIAAAAAAAACoVSwW6jMBD9ladoD7BBtHuFddUeetjzHkNUuWRIrDrjamLajSL%2BvbINNNUi9QKaN%2B8Nbx5zWbVuR6tq1Vp9OuGPJ8GlYeDnnjjTInkqgc4JMkseBgq3NQx%2BQ4uUlnjvDzXMej1zgbMhuwv9jdnWCRzCKz6MJ%2Fky%2B3NsqlvHJw%2FXeyhc8KZtTxW4t7bAzjFV6LQ9EYYv9DA18CcLm7%2Fn47OzZcC1d7KtkOVQd5FYTCymf35qzFLAdMgMlFJXO%2BbBURkMQMFLT0UEoj2o6K%2F%2BnEDB4XU%2FhrFeb684Qr4XDqwZHMawRmBkBMv1mN%2FQcMNp5R%2FtoecXqNH%2Fg4g%2Bl524Y3ZJliv8uh0KZE8FTNo9r2ex4deY8GZbBHNQIzQzxkSZ3uNh1OHDNze4fyZuD0ctL9gTp3Ab7pxkSUZwXdSW0w3lC9Lptywr5xNZknZOytJ1%2FykDf5meqN9d73yuCxMedXtoOEjGIqN4MkNer4YP3LYJCEYDAAA%3D)

One would argue that `for..of` in Chrome performs nice despite of using an array's iterator but that's an optimization of using a native iterator. Firefox is generally more honest in benchmarks if we want generic evaluation of Javascript code, but about that is in the next post.