for loop vs $.each (v19)

Revision 19 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++) {}
ready
$.each
$set.each(function(i, e) {});
ready
my each
myeach($set, function(i, l) {});
ready
better for
var i = 0,
    len = $set.length;

for ( i; i < len; i++) {}
ready

Revisions

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