iterators

JavaScript performance comparison

Test case created by EricRabil

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const array = window.array = [];
  
  for (var i = 0; i < 10000; i++) {
    array.push(i);
  }
  
  const array1 = window.array1 = [];
  
  Array.prototype.altForEach = function(iter) {
    let index = -1;
    while (index++ < (array.length -1)) {
      iter(this[index], index, this)
    }
  }
  
  Array.prototype.aforEach = function(cb) {
    return new Promise(resolve => {
  const pending = [];  
  for (let index = 0; index < this.length; index++) {
      pending.push((() => new Promise(r => r(cb(this[index], index, this))))());
  }
  resolve(Promise.all(pending));
  });
  }

};
</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
traditional for
for (let i = 0; i < array.length; i++) {
  array1.push(array[i]);
}
pending…
for of
for (let i of array) {
  array1.push(i);
}
pending…
forEach
array.forEach(i => array1.push(i));
pending…
forEach patched
array.altForEach(i => array1.push(i));
pending…
while
let index = -1;
while (index < array.length) {
  array1.push(array[index++]);
}
pending…
async
// async test
array.aforEach(ele => array1.push(ele)).then(() => deferred.resolve());
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.

0 Comments