JQuery each vs for loop vs _.each (lodash)

JavaScript performance comparison

Revision 9 of this test case created by Jorge Castro

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script>
  fatArray = [];
  size = 1000;
  for(var i = 0; i < size; i++) {
    fatArray[i] = Math.floor(Math.random() * size);
  }
</script>
<script>
Benchmark.prototype.setup = function() {
    var min = Infinity;
   
    function compare(index, value) {
      if (min > value) {
        min = value;
      }
    }
};
</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(fatArray, compare);
pending…
for
for (var i = 0; i < fatArray.length; i += 1) {
  var val = fatArray[i];
  if (min > val) {
    min = val;
  }
}
pending…
_.each
_.each(fatArray, compare);
pending…
forEach
fatArray.forEach(compare);
pending…
while
var i = fatArray.length;
while (i--) {
  var val = fatArray[i];
  if (min > val) {
    min = val;
  }
}
pending…
for length
var val;
for (var i = 0, l = fatArray.length; i < l; i++) {
  val = fatArray[i];
  if (min > val) {
    min = val;
  }
}
pending…
outside Lenght
var val,
  iLenght = fatArray.length;
for (var i = 0, l = iLenght; i < l; i++) {
  val = fatArray[i];
  if (min > val) {
    min = val;
  }
}
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