While vs For Queue vs jq.each

JavaScript performance comparison

Revision 58 of this test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var items = [],
      total = 0,
      i = 0,
      j = 0;
    for (; i < 100000; i++) {
      items[i] = Math.ceil(Math.random() * 10);
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
For
for (; j < items.length; j++) {
  total += items[j];
}
pending…
While
while (items.length) {
  total += items.shift();
}
pending…
Do
do {
  total += items.shift();
} while (items.length);
pending…
jquery.each
$.each(items, function(index, item) {
  total += item;
});
pending…
normal while
var i = items.length;
while (i > 0) {
  total += items[i - 1];
  i--;
}
pending…
jquery each 2
$(items).each(function(index, item) {
  total += item;
});
pending…
var i = items.length;
while (i--) {
  total += items[i];
}
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