jQuery each vs JS forEach (v11)

Revision 11 of this benchmark created on


Description

This is just a test I did to see which is faster, the JS version or the jQuery version. Both are Javascript based, but I just wanted to see whether jQuery got away with something when it comes down to performance and memory management

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
  // Setup collection
  var collection = [];
  for (var i = 0; i < 20000; i++) {
    collection.push({
      index: i,
      data: 'Index element ' + i
    });
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery
$.each(collection, function(index, element) {
     console.log( index,"SSSSSSSSSSSSSSSSSSSSSSSSSS");
});
ready
JS forEach()
collection.forEach(function(element, index) {
    console.log(index,"SSSSSSSSSSSSSSSSSSSSSSSSSS");
});
ready
JS for(var x in collection)
for (var index in collection) {
  if (collection.hasOwnProperty(index)) 
    console.log(index,"SSSSSSSSSSSSSSSSSSSSSSSSSS");
  }
ready
Js for
for (var i=0,len=collection.length; i<len ; i++) {
    console.log(i,"SSSSSSSSSSSSSSSSSSSSSSSSSS");
  }
  
ready

Revisions

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