generator-vs-array

JavaScript performance comparison

Revision 3 of this test case created

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++]
      );
    }
    function forLoop() {
      for(var
        out = [],
        i = 0; i < this.length;
        out.push(this[i++])
      );
      return out;
    }
    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 iterable = generator.call(arr),
    values = [],
    value;
while (!(value = iterable.next()).done) {
  values.push(value.value);
}
pending…
for loop
var values = forLoop.call(arr);
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