Traceur array iteration: for-of vs for vs forEach

JavaScript performance comparison

Test case created by

Preparation code

<script src="http://me.kwhq.net/traceur/runtime.js"></script>
<!-- Using my server because github forces plaintext and Chrome won't run the script -->
      
<script>
Benchmark.prototype.setup = function() {
  var array = [], a = 0;
  for(var k = 0; k < 100; k++) array[k] = Math.random();
  
  function $__getIterator1(collection) {
      if(Array.isArray(collection)) {
        var i = 0, obj = {value: void 0, done: false};
        return {
          next: function() {
            if (i < collection.length)
              obj.value = collection[i++];
            else {
              obj.value = void 0;
              obj.done = true;
            }
            return obj;
          }
        };
      }
      return $traceurRuntime.getIterator(collection);
  }
  
  function $__getIterator2(collection) {
      if(Array.isArray(collection)) {
        var i = 0;
        return {
          value: void 0, done: false,
          next: function() {
            if (i < collection.length)
              this.value = collection[i++];
            else {
              this.value = void 0;
              this.done = true;
            }
            return this;
          }
        };
      }
      return $traceurRuntime.getIterator(collection);
  }
  
  function $__ArrayIter(a) {
      this.a = a;
      this.i = 0;
      this.value = void 0;
      this.done = false;
  }
  
  $__ArrayIter.prototype.next = function next() {
      if(this.i < this.a.length)
          this.value = this.a[this.i++];
      else {
          this.value = void 0;
          this.done = true;
      }
      return this;
  }
  
  function $__getIterator3(collection) {
      if(Array.isArray(collection))
          return new $__ArrayIter(collection);
      return $traceurRuntime.getIterator(collection);
  }

};
</script>

Preparation code output

<!-- Using my server because github forces plaintext and Chrome won't run the 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
for-of
for (var $__0 = $traceurRuntime.getIterator(array), $__1; !($__1 = $__0.next()).done;) {
  var x = $__1.value;
  a += x;
}
pending…
for-of, special array case w/ object reuse
for (var $__0 = $__getIterator1(array), $__1; !($__1 = $__0.next()).done;) {
  var x = $__1.value;
  a += x;
}
pending…
for-of, special array case w/ array iterator class
for (var $__0 = $__getIterator2(array), $__1; !($__1 = $__0.next()).done;) {
  var x = $__1.value;
  a += x;
}
pending…
for
for (var i = 0; i < array.length; i++) a += array[i];
pending…
for, cached length
for (var i = 0, l = array.length; i < l; i++) a += array[i];
pending…
forEach
array.forEach((function(x) {
  a += x;
}));
pending…
for-of, special array case w/ more object reuse
for (var $__0 = $__getIterator2(array), $__1; !($__1 = $__0.next()).done;) {
  var x = $__1.value;
  a += x;
}
pending…

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

Compare results of other browsers

0 Comments