jQuery vs JavaScript Performance Comparison (v14)

Revision 14 of this benchmark created on


Preparation HTML

<div id="hello"></div>
<div class="bye"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

var allEls = document.getElementsByTagName("*"),
      i,
      idHash = { 'tag' : [], 'class' : [], 'id' : [] };
    
    for( i in allEls ){
      var thisEl = allEls[i],
          classes = (thisEl.className) ? thisEl.className.split(' ') : [];
    
      idHash.tag[thisEl.tagName] = idHash.tag[thisEl.tagName] || [];    
      idHash.tag[thisEl.tagName].push( thisEl );
      
      for( j in classes ){
        var classStr = classes[j];
        
        idHash.class[classStr] = idHash.class[classStr] || [];
        idHash.class[classStr].push( thisEl );
      }
    
      idHash.id[thisEl.id] = thisEl;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery ID Selector
var $el = $('#hello'); // jQuery object
ready
JavaScript ID Selector
var el = document.querySelector('#hello'); // DOM element
ready
jQuery Class Selector
var $el = $('.bye'); // jQuery object
ready
JavaScript Class Selector
var el = document.querySelector('.bye'); // DOM elements
ready
get element by id
var el = document.getElementById('hello'); // DOM element
ready
get element by id to Jquery
var el = document.getElementById('hello'), // DOM element
   $el = $(el);                            // jQuery object
ready
ID Selection: Frontload ID assignments, cache, then read from cache
var el = idHash.id['hello'];
ready
Class Selection: Frontload Class assignments, cache, then read from cache
var el = idHash.class['bye'];
ready

Revisions

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