Underscore.each vs jQuery.each vs. for loop vs. `Lo-Dash.each'

JavaScript performance comparison

Revision 129 of this test case created

Info

Added [].forEach

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

<script src="//documentcloud.github.com/underscore/underscore-min.js">
</script>
<script>
window.underscore = _.noConflict();
</script>
<script src="https://raw.github.com/bestiejs/lodash/master/lodash.min.js">
</script>
<script>
window.lodash = _.noConflict();
</script>
 
<script>
Benchmark.prototype.setup = function() {
    var $ = window.$,
        lodash = window.lodash,
        underscore = window.underscore;
   
    var a = [],
        newArray = [],
        pi = Math.PI;
   
    for (var i = 1, max= 100 ; i <= max ; i++) {
      a.push('Fundamental alert number ' + i);
    }
   
};
</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(index, val) {
  newArray.push(val);
});
pending…
good old for loop
for (var i = 0, len = a.length; i < len; i++) {
  newArray.push(
    a[i]
  );
}
pending…
underscore.each
underscore.each(a, function(item) {
  newArray.push(
    item
  );
});
pending…
Lo-Dash each
lodash.each(a, function(item) {
  newArray.push(
    item
  );
});
pending…
underscore(a).each
underscore(a).each(function(item) {
  newArray.push(
    item
  );
});
pending…
good old for loop ++
for (var b = a[0], len = a.length; len; b = a[--len]) {
  newArray.push(
    b
  );
}
pending…
[].foreach()
a.forEach(function(item) {
  newArray.push(
    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