loop-comparison2

JavaScript performance comparison

Test case created by samhuk

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const A = []
  for (let i = 0; i < 1000; i++)
    A.push({ a: Math.random(), b: Math.random() })
  
  // ---------
  
  function customForEach(array, fn) {
    for (let i = 0; i < array.length; i++) {
      fn(array[i]);
    }
  }

};
</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
const B = [];
for (a of A) {
  B.push(a.a + a.b);
}
pending…
for
const B = []
for (let i = 0; i < A.length; i++) {
  B.push(A[i].a + A[i].b)
}
pending…
for (length var)
const B = [];
const A_length = A.length;
for (let i = 0; i < A_length ; i++) {
  B.push(A[i].a + A[i].b);
}
pending…
foreach
const B = [];
A.forEach(function(a) { B.push(a.a + a.b) });
pending…
custom-foreach
const B = [];
customForEach(A, function(a) { B.push(a.a + a.b) })
pending…
while
const B = []
let i = 0
while (i < A.length) {
  B.push(A[i].a + A[i].b)
  i++
}
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