jQuery.each vs. for loop

JavaScript performance comparison

Revision 218 of this test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>

<script src="//underscorejs.org/underscore.js"></script>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
  var a = $('*').get(),
      e;
</script>
<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js">
</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
jQuery.each
$.each(a, function() {
  e = this;
});
pending…
for loop
var len;
for (var i = 0, len = a.length; i < len; i++) {
  e = a[i];
};
pending…
for without caching
for (var i = 0; i < a.length; i++) {
  e = a[i];
};
pending…
alternative for loop
for (var i in a) {
  e = a[i];
};
pending…
foreach
a.forEach(function(elem) {
  e = elem;
});
pending…
knockout arrayForEach
ko.utils.arrayForEach(a, function(item) {
  e = item;
});
pending…
angular
angular.forEach(a, function(item) {
  e = item;
});
pending…
underscore
_.forEach(a, function(item) {
  e = 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:

0 comments

Add a comment