Fastest array loops in Javascript (v321)

Revision 321 of this benchmark created on


Preparation HTML

<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

Ready to run.

Testing in
TestOps/sec
While loop, basic
var i = 0;
while (i < arr) {
  someFn(i);
  i++;
}
ready
For loop, cached
for (var i = 0, len = arr; i < len; i++) {
  someFn(i);
}
ready
For loop, i--
for (var i = arr; i > 0; i--) {
  someFn(i);
}
ready
Do-while loop, i--
var i = arr - 1;
do {
  someFn(i);
}
while (i--);
ready
Do-while loop, --i
var i = arr;
if (i > 0) {
  do {
    someFn(i);
  }
  while (--i);
}
ready
while loop i--
var i = arr;
while (i--) {
  someFn(i);
}
ready
For loop, i -= 1
for (var i = arr; i > 0; i -= 1) {
  someFn(i);
}
ready
For loop, i -= 1 II
var i;
for (i = arr; i > 0; i -= 1) {
  someFn(i);
}
ready
For loop
var i;
for (i = arr; i > 0; i--) {
  someFn(i);
}
ready
For loop, basic
for (var i = 0; i < arr; i++) {
  someFn(i);
}
ready

Revisions

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