forEach vs loop vs ordinary loop

JavaScript performance comparison

Revision 48 of this test case created by Igor

Info

I've added normal for i loop

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.5/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.3.1/lodash.min.js"></script>
<script>
var lodash = _.noConflict();
</script>
<script>
Benchmark.prototype.setup = function() {
    var _ = window._,
       angular = window.angular,
       lodash = window.lodash;
   
    var arr = [];
    for(var i = 0; i < 100; i++) {
      arr[i] = i;
    }
   
    var values = [];
};
</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
Array#forEach
arr.forEach(function(value) {
  values.push(value);
});
pending…
underscore.forEach
_.forEach(arr, function(value) {
  values.push(value);
});
pending…
lodash.forEach
lodash.forEach(arr, function(value) {
  values.push(value);
});
pending…
angular.forEach
angular.forEach(arr, function(value) {
  values.push(value);
});
pending…
Test 5
for (var i = 0; i < arr.length; i++) {
   values.push(arr[i]);
}
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:

0 comments

Add a comment