for loop vs $.each (v23)

Revision 23 of this benchmark created on


Description

<div id="id_1"> <span>foo0</span> <span>foo1</span> <span>foo2</span> <span>foo3</span> <span>foo4</span> <span>foo5</span> <span>foo6</span> <span>foo7</span> <span>foo8</span> </div>

Preparation HTML

<div id="id_1">
  <span>
    foo0
  </span>
  <span>
    foo1
  </span>
  <span>
    foo2
  </span>
  <span>
    foo3
  </span>
  <span>
    foo4
  </span>
  <span>
    foo5
  </span>
  <span>
    foo6
  </span>
  <span>
    foo7
  </span>
  <span>
    foo8
  </span>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
  var $set = jQuery('#id_1').children();

  function myeach(arr, func) {
    for (var i = 0; i < arr.length; i++) {
      func(i, arr[i]);
    }
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
for
for (var i = 0, len = $set.length; i < len; i++) { b = $set[i];}
ready
for 2
var len = $set.length;
for (var i = 0; i < len; i++) { b = $set[i];}
ready
for (slow)
for (var i = 0; i < $set.length; i++) { b = $set[i];}
ready
jQuery .each()
$set.each(function(i, e) {b = $set[i];});
ready
for(var in set)
for (c in $set) { b = c;}
ready

Revisions

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