generator-vs-array

JavaScript performance comparison

Revision 5 of this test case created by Otto Lopez Valerio

Info

trying simple and probably unfair performance comparison between generators and just arrays

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = Array(100).join(Math.random()).split('.');
    function* generator() {
      for(var
        i = 0; i < this.length;
        yield this[i++]
      );
    }
    var genvar0 = 0;
    function genNext() {
        var i = genvar0;
        genvar0 = i + 1;
        if (i <= this.length) {
           return this[i];
        } else {
           return null;
        }
    }
    function forEach(value, key, original) {
      this.push(value);
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
generator
var values = (i for (i in generator.call(arr)));
console.log(values.length);
 
pending…
simulated generator
var values = [];
while (true) {
   var value = genNext.call(arr);
   if (value) {
      values.push(value);
   } else {
     break;
   }
}
pending…
forEach
var values = [];
arr.forEach(forEach, values);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment