JQuery $('#id') vs jQuery(document.getElementById('id')); (v27)

Revision 27 of this benchmark created on


Description

Fastest way get a jQuery object of a DOM element of which I have the ID. document.getElementById has the advantage of not requiring escaping for special characters.

Preparation HTML

<div class="class">Some HTML element</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery('class')
jQuery('.class');
ready
jQuery(getElementsByClass(Classname))
function getElementsByClass(Classname) {
    var classElm = new Array();
    var allElm = document.getElementsByTagName("*");
    for (i = 0, j = 0; i < allElm.length; i++) {
        if (allElm[i].className == Classname) {
            classElm[j] = allElm[i];
            j++;
        }
    }
    return classElm;
}

$(getElementsByClass('class'));
ready

Revisions

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