jQuery each vs JS forEach (v5)

Revision 5 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 < 2000; 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) {
  if (typeof console !== 'undefined') {
    console.log('jQuery: index', index, 'element', element);
  }
});
ready
JS forEach()
collection.forEach(function(element, index) {
  if (typeof console !== 'undefined') {
    console.log('JS: index', index, 'element', element);
  }
});
ready
JS for(var x in collection)
for (var index in collection) {
  if (collection.indexOf(index) !== -1 || collection.hasOwnProperty(index)) {
    console.log('JS: index', index, 'element', collection[index]);
  }
  }
ready

Revisions

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