jQuery.each vs _.each vs for loop

JavaScript performance comparison

Revision 16 of this test case created by LocalPCGuy

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js">
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.3.1/lodash.min.js">
</script>
<script>
  var lodash = _.noConflict();
  var array = $('*').get();
  var eachFunc = function(element, index, list) {
    e = element;
  };

</script>
<script>
Benchmark.prototype.setup = function() {
    var $ = window.$;
    var _ = window._;
    var lodash = window.lodash;
   
    var e;
    var array = window.array;
};
</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(array, eachFunc);
pending…
_.each (underscore)
_.each(array, eachFunc);
pending…
_.each (lodash)
lodash.each(array, eachFunc);
pending…
for loop
for (var i = 0, len = array.length; i < len; i++) {
  e = array[i];
}
pending…
cached iterator 'for' loop
var i;
for (i = 0, len = array.length; i < len; i++) {
  e = array[i];
}
pending…
'for in' loop
var i;
for (i in array) {
  e = array[i];
}
pending…
$.each (jQuery) w/anonymous function
$.each(array, function(element, index) {
   e = element;
});
pending…
_.each (underscore) w/anonymous function
_.each(array, function(element, index, list) {
   e = element;
});
pending…
_.each (lodash) w/anonymous function
lodash.each(array, function(element, index, list) {
   e = element;
});
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