JS loop comparison with lodash

Benchmark created by Zack Taylor 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(10000);

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
Lodash foreach
_.forEach(arr, function(value, 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--;) {
  arr[i];
};
ready
Angular foreach
angular.forEach(arr, function(value, i) {
  arr[i];
});
ready
Native foreach
arr.forEach(function(value, i) {
  arr[i];
});
ready
for loop
for (var i = 0; i < arr.length; ++i) {
  arr[i];
};
ready
Uglified cached for loop
for(var i=0,len=arr.length;len>i;++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 Zack Taylor on
  • Revision 2: published on
  • Revision 3: published by Daniel Joppi on