Fastest array loops in Javascript (v122)

Revision 122 of this benchmark created by Misko Hevery on


Preparation HTML

<script src="https://code.jquery.com/jquery-git2.min.js"></script>

Setup

// Populate the base array
  var arr = [];
  for (var i = 0; i < 20; i++) {
    arr[i] = (Math.random() * 1000 |0);
  }
  
  function someFn(ix) {
    return ix << 1;
  }
  var len = arr.length, i = 0, arr2 = arr.slice();

Teardown



            i = 0; // reset counters if modified
  arr = arr2.slice(); // repopulate by copying back
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
For loop, basic
var count = 0;
for (var i = 0; i < arr.length; i++) {
  count += arr[i];
}
return count;
ready
While loop, basic
var count = 0;
while (i < len) {
  count += arr[i];
  i++;
}
return count;
ready
For loop, cached+init
var count = 0;
for (var i = 0; i < len; i++) {
  count += arr[i];
}
return count;
ready
For loop, i--
var count = 0;
for (var i = len; i > 0; i--) {
  count += arr[i];
}
return count;
ready
Do-while loop, i--
var count = 0;
var i = len - 1;
do {
  count += arr[i];
} while (i--);
return count;
ready
Do-while loop, --i
var count = 0;
var i = len;
if (i > 0) {
  do {
    count += arr[i];
  }
  while (--i);
}
return count;
ready
For..in loop
var count = 0;
for (var i in arr) {
  count += arr[i];
}
return count;
ready
shift 1
var count = 0;
while (i = arr.shift()) {
  count += arr[i];
}
return count;
ready
forEach
var count = 0;
arr.forEach(function(i) {
  count += i;
});
return count;
ready
shift 2
var count = 0;
while ((i = arr.shift()) !== undefined) {
  count += arr[i];
}
return count;
ready
While ! Undefined
var count = 0;
var a;
while ((a = arr[i++]) !== undefined) {
  count += arr[a];
}
return count;
ready
Pop
var count = 0;
while ((i = arr.pop()) !== undefined) {
  count += arr[i];
}
return count;
ready
Order w/ array copy
var count = 0;
var copy = arr.slice(0);
while (i = copy.shift()) {
  count += arr[i];
}
return count;
ready
While length--
var count = 0;
var l = len;
while (l--) {
  count += arr[l];
}
return count;
ready
pop2
var count = 0;
var i = arr.pop();
while (i !== undefined) {
  count += arr[i];
  i = arr.pop();
}
return count;
ready
non-init for
var i;
var count = 0;
for (; i < len; ++i) {
  count += arr[i];
}
return count;
ready
Object.keys
var count = 0;
Object.keys(arr).forEach(function(i) {
	count += arr[i];
});
return count;
ready
while non cached len
var i;
var count = 0;
while (i < arr.length) {
  count += arr[i];
  i++;
}
return count;
ready
while obj.keys.length
var count = 0;
var i = Object.keys(arr).length;
while (i--) {
  count += arr[i];
}
return count;
ready
jQuery
var count = 0;
$.each(arr, function(index, value) {
 count += value;
});
return count;
ready
pop3
var count = 0;
while (i = arr.pop()) {
  count += arr[i];
}
return count;
ready
shift 3
var count = 0;
var i = arr.shift();
while (i !== undefined) {
  count += i;
  i = arr.shift();
}
return count;
ready
noncached inline while
var count = 0;
while (i++ < arr.length) {
  count += arr[i];
}
return count;
ready

Revisions

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