Generator vs loops

JavaScript performance comparison

Test case created by kwasimensah

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const total = 1000000;
  
  function calculateIterativelyArray(nums) {
    let sum = 0;
    let num;
    for (num of nums) {
      sum += num;
    }
    return sum;
  }
  
  function calculateIterativelyGenerator(nums) {
    let sum = 0;
    let num;
    for (num of nums) {
      sum += num;
    }
    return sum;
  }

};
</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
array
const numArray = [];
for (let i = 0; i < total; ++i) {
  numArray.push(i);
}

calculateIterativelyArray(numArray);
pending…
Generator
function* numGenerator() {
  for (let i = 0; i < total; ++i) {
    yield i;
  }
}

calculateIterativelyGenerator(numGenerator());
pending…

Revisions

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

0 Comments