AsyncGenerators vs Recursion

JavaScript performance comparison

Test case created by maraisr

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  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;
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
AsyncGenerators
// async test
(async function () {
  for await (const item of generate()) {
    console.log(item);
  }
  deferred.resolve();
})();
pending…
Recursion
// async test
(async function () {
  const result = await recurse();
  result.forEach((val) => console.log(val));
  deferred.resolve();
})();
pending…

Revisions

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

0 Comments