Fastest array loops in Javascript

JavaScript performance comparison

Revision 321 of this test case created by

Preparation code

<script>
  // Populate the base array
  var arr = 2147483647;

  function someFn(num) {
  if(num===0) return [[0,0]];
  if(num===1) return [[0,1]];
  var result = [];
  for(var i=0;i*i<=num;i++){
    for(var j=i;j*j<=num;j++){
      if((i*i + j*j) === num){
        result.push([i,j]);
      }
    }
  }
  return result;
}
</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
While loop, basic
var i = 0;
while (i < arr) {
  someFn(i);
  i++;
}
pending…
For loop, cached
for (var i = 0, len = arr; i < len; i++) {
  someFn(i);
}
pending…
For loop, i--
for (var i = arr; i > 0; i--) {
  someFn(i);
}
pending…
Do-while loop, i--
var i = arr - 1;
do {
  someFn(i);
}
while (i--);
pending…
Do-while loop, --i
var i = arr;
if (i > 0) {
  do {
    someFn(i);
  }
  while (--i);
}
pending…
while loop i--
var i = arr;
while (i--) {
  someFn(i);
}
pending…
For loop, i -= 1
for (var i = arr; i > 0; i -= 1) {
  someFn(i);
}
pending…
For loop, i -= 1 II
var i;
for (i = arr; i > 0; i -= 1) {
  someFn(i);
}
pending…
For loop
var i;
for (i = arr; i > 0; i--) {
  someFn(i);
}
pending…
For loop, basic
for (var i = 0; i < arr; i++) {
  someFn(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