AsyncGenerators vs Recursion

Benchmark created by maraisr on


Setup

let x = -1;
  
  const thing = {
    read() {
      ++x;
      if (x !== 10) {
        return Promise.resolve(x);
      }
      return null;
    },
  };
  
  async function* generate() {
    while (true) {
      const val = await thing.read();
      if (val !== null) yield val;
      break;
    }
  }
  
  async function recurse(returns = []) {
    const val = await thing.read();
    if (val !== null) {
     	returns.push(val);
      return recurse(returns);
    }
    return returns;
  }

Test runner

Ready to run.

Testing in
TestOps/sec
AsyncGenerators
// async test
(async function () {
  for await (const item of generate()) {
    console.log(item);
  }
  deferred.resolve();
})();
ready
Recursion
// async test
(async function () {
  const result = await recurse();
  result.forEach((val) => console.log(val));
  deferred.resolve();
})();
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.