jQuery 1.7 each vs native for loop (v3)

Revision 3 of this benchmark created by Kevin Smith on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js">
</script>
<ul>
  <li>
    Item 01
  </li>
  <li>
    Item 02
  </li>
  <li>
    Item 03
  </li>
  <li>
    Item 04
  </li>
  <li>
    Item 05
  </li>
  <li>
    Item 06
  </li>
  <li>
    Item 07
  </li>
  <li>
    Item 08
  </li>
  <li>
    Item 09
  </li>
  <li>
    Item 10
  </li>
  <li>
    Item 11
  </li>
  <li>
    Item 12
  </li>
  <li>
    Item 13
  </li>
  <li>
    Item 14
  </li>
  <li>
    Item 15
  </li>
  <li>
    Item 16
  </li>
  <li>
    Item 17
  </li>
  <li>
    Item 18
  </li>
  <li>
    Item 19
  </li>
  <li>
    Item 20
  </li>
</ul>
<script>
  var $listItems = $('li');
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
native for
var length = $listItems[length];
for (var i = 0; i < length; i++) {
  console.log("From for: ", $($listItems[i]));
}
ready
jQuery 1.7 .each()
$listItems.each(function() {
  console.log("From each(): ", $(this));
});
ready
jQuery 1.7 $.each()
$.each($listItems, function(index, element) {
  console.log("From $.each():" + $(element));
});
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.