jQuery.each vs _.each vs for loop vs array.forEach vs Lazy.each

JavaScript performance comparison

Revision 42 of this test case created by zsolt

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
<script src="https://rawgithub.com/dtao/lazy.js/master/lazy.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.0.1/lodash.min.js"></script>
<script>
  var lodash = _.noConflict();
  var array = $('*').get();
  var callback = function(item, index) {
    e = item;
  };
var callbackUnderscore = function(item, index) {
    e = item;
  };
  var callbackLodash = function(index, item) {
    e = item;
  };
  var callbackLazy = function(index, item) {
    e = item;
  };
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var $ = window.$;
  var _ = window._;
  var lodash = window.lodash;
  var Lazy = window.Lazy;
  
  var e;
  var array = window.array;

};
</script>

Preparation code output

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script> <script src="https://rawgithub.com/dtao/lazy.js/master/lazy.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.0.1/lodash.min.js"></script> <script> var lodash = _.noConflict(); var array = $('*').get(); var callback = function(item, index) { e = item; }; var callbackUnderscore = function(item, index) { e = item; }; var callbackLodash = function(index, item) { e = item; }; var callbackLazy = function(index, item) { e = item; }; </script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
for in loop
for (var i  in array) { if (array.hasOwnProperty(i)) { e = array[i]; } }
pending…
Lazy each
Lazy(array).each(callbackLazy);
pending…
Array.map
array.map(callback);
pending…
_.each (underscore)
_.each(array, callbackUnderscore);
pending…
_.each (lodash)
lodash.each(array, callbackLodash );
pending…
for loop
for (var i = 0, len = array.length; i < len; i++) { e = array[i]; }
pending…
array.forEach
array.forEach(callback);
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.

0 Comments