AngularJS forEach vs Native forEach vs key vs for

JavaScript performance comparison

Revision 16 of this test case created

Info

Compare performance of the following iterative patterns:

AngularJS angular.forEach Native array.forEach Native for key in arr Native for loop

Should be quicker when caching length, also updated latest stable AngularJS! :)

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js">
</script>
<script>
  var len = 1000;
  var arr = new Array();
  while (len--) {
    arr[len] = len;
  }
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Native forEach
arr.forEach(function(item) {
  item;
});
pending…
AngularJS forEach
angular.forEach(arr, function(item) {
  item;
});
pending…
for key in arr
for (var key in arr) {
  arr[key];
}
pending…
for loop
for (i = arr.length - 1; i >= 0; i--) {
  arr[i];
}
pending…
for loop, super-cached i and arr length, simpler!
var i,arrLen = arr.length;
for (i = 0; i < arrLen; i++) {
  arr[i];
}
pending…
for loop, simple (with var)
for (var i = 0; i < arr.length; i++) {
  arr[i];
}
pending…
_
_.forEach(arr, function(item) {
  item;
});
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

1 comment

Tom Taylor commented :

lodash - faster than angularJs, but still shit!

Add a comment