Fastest array loops in Javascript (v282)

Revision 282 of this benchmark created on


Preparation HTML

<script>
  // Populate the base array
  var arr = [];
  for (var i = 0; i < 1000; i++) {
    arr[i] = 'value' + i;
  }

  function someFn(ix) {
   var  i =  ix * 5 + 1 / 3 * 8;
    m +=i;
return i;
  }
</script>

Setup

m = 0;

Teardown



            console.log(m);
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
For loop
var i;
for (i = arr.length; i > 0; i--) {
  someFn(i);
}
ready
forEach function
arr.forEach(function(d, i) {
  someFn(i);
});
ready
Removing Element while loop
var ar = arr;
var l = ar.length;
while (ar.length) {

  someFn(10);
  ar.shift();
}
ready
For loop, basic
for (var i = 0; i < arr.length; i++) {
  someFn(i);
}
ready
For loop, i -= 1
for (var i = arr.length; i > 0; i -= 1) {
  someFn(i);
}
ready
while loop i--
var i = arr.length;
while (i--) {
  someFn(i);
}
ready

Revisions

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