JS loop comparison (v6)

Revision 6 of this benchmark created by Benjamin Reed on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.4/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>

Setup

var arr = new Array(100);
    for (var ix = 0; ix < arr.length; ++ix) {
      arr[ix] = ix;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
while loop that imitates a for loop
var i = 0;
while (i < arr.length) {
  arr[i];
  i++;
};
ready
cached while loop
var i = 0,
  len = arr.length;
while (i < len) {
  arr[i];
  i++;
};
ready
reverse while loop
var i = arr.length;
while (i) {
  i--;
  arr[i];
};
ready
for loop
for (var i = 0; i < arr.length; ++i) {
  arr[i];
};
ready
cached for loop
var i = 0,
  len = arr.length
for (; i < len; ++i) {
  arr[i];
};
ready
reverse for loop
for (var i = arr.length; i; ) {
  i--;
  arr[i];
};
ready
Angular foreach
angular.forEach(arr, function(value, i) {
  arr[i];
});
ready
Native foreach
arr.forEach(function(value, i) {
  arr[i];
});
ready
Lodash forEach
_.forEach(arr, function(value, i) {
  arr[i];
});
ready
for ... in
// WARNING - this loop ignore empty values
for (var i in arr) {
  arr[i];
};
ready
for loop with length var
for (var i = 0, len = arr.length; i < len; ++i) {
  arr[i];
};
ready

Revisions

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

  • Revision 1: published by shprink on
  • Revision 2: published by shprink on
  • Revision 3: published by shprink on
  • Revision 4: published by Daniel Joppi on
  • Revision 6: published by Benjamin Reed on