jquery loop vs native loop

Benchmark created on


Description

How much faster is a native for loop, compared to jQuery.each(), when looping through a simple array?

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var myArray = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'];
  
  function doSomething(i, el) {}
  
  function doAnotherLoop(i, el) {
   $.each(myArray, doSomething);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery
$.each(myArray, doSomething);
ready
Native
for (var i = 0, length = myArray.length; i < length; i++) {
 doSomething()
}
ready
Nested jQuery
$.each(myArray, doAnotherLoop);
ready
Nested Native
for (var i = 0, length = myArray.length; i < length; i++) {
 for (var ii = 0, childLength = myArray.length; ii < childLength; ii++) {
  doSomething()
 }
}
ready
old
 
ready

Revisions

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