jQuery.each vs. for loop vs Array.forEach (v257)

Revision 257 of this benchmark created by Zev Benjamin on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var a = $('*').get();
  function naive_foreach(lst, func) {
    for (var i = 0, len = lst.length; i < len; i++) {
      func(lst[i]);
    }
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery.each
$.each(a, function() {
 var e = this;
});
ready
for loop
for (var i = 0, len = a.length; i < len; i++) {
 var e = a[i];
};
ready
Array.forEach
Array.prototype.forEach.call(a, function(e) {});
 
ready
Naive forEach
naive_foreach(a, function(e) {});
ready

Revisions

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