jQuery each vs JS forEach

Benchmark created by John 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 < 200; 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
collection.forEach(function(element, index) {
  if( typeof console !== 'undefined' ) {
    console.log('JS: index', index, 'element', element);
  }
});
 
ready

Revisions

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