querySelector vs querySelectorAll vs getElementById vs getElementsByClassName vs getElementsByTagName vs getElementsByName (v52)

Revision 52 of this benchmark created by Skateside on


Description

As getElementById, getElementsByTagName and getElementsByClassName are consistently faster than querySelector and querySelectorAll, is it faster to check a CSS string and pass to one of these than simply defaulting to querySelectorAll? All results are converted into an array for more consistency in results.

Preparation HTML

<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div id="test">test</div>

Setup

var dom = {
    
        toArray: function (nodes) {
    
            var array = [];
    
            if (nodes) {
    
                array = typeof nodes.length === 'number' ?
                        Array.prototype.slice.call(nodes) :
                        [nodes];
    
            }
    
            return array;
    
        },
    
        byId: function (id, context) {
    
            var start = context || document;
    
            return this.toArray(start.getElementById(id));
    
        },
    
        byClass: function (className, context) {
    
            var start = context || document;
    
            return this.toArray(start.getElementsByClassName(className));
    
        },
    
        byTag: function (tag, context) {
    
            var start = context || document;
    
            return this.toArray(start.getElementsByTagName(tag));
    
        },
    
        byQuery: function (query, context) {
    
            var start = context || document;
    
            return this.toArray(start.querySelectorAll(query));
    
        },
    
        get: function (query, context) {
    
            var found = null,
                name  = query.slice(1);
    
            if (/^#\w+$/.test(query)) {
                found = this.byId(name, context);
            } else if (/^\.\w+$/.test(query)) {
                found = this.byClass(name, context);
            } else if (/^\w+$/.test(query)) {
                found = this.byTag(query, context);
            } else {
                found = this.byQuery(query, context);
            }
    
            return found;
    
        },
    
        get2: function (query, context) {
    
            var reg   = /(^#\w+$)|(^\.\w+$)|(^\w+$)/,
                match = query.match(reg),
                name  = query.slice(1),
                found = null;
    
            if (match) {
    
                found = match[1] ?
                        this.byId(name, context) :
                        match[2] ?
                                this.byClass(name, context) :
                                this.byTag(query, context);
    
            } else {
                found = this.byQuery(query, context);
            }
    
            return found;
    
        }
    
    };

Test runner

Ready to run.

Testing in
TestOps/sec
getElementById
dom.toArray(document.getElementById('test'));
ready
getElementsByClassName
dom.toArray(document.getElementsByClassName('test'));
ready
getElementsByTagName
dom.toArray(document.getElementsByTagName('div'));
ready
querySelectorAll
dom.toArray(document.querySelectorAll('div#test'));
ready
get (ID)
dom.get('#test');
ready
get (class)
dom.get('.test');
ready
get (tag)
dom.get('div');
ready
get (query)
dom.get('div#test');
ready
get2 (ID)
dom.get2('#test');
ready
get2 (class)
dom.get2('.test');
ready
get2 (tag)
dom.get2('div');
ready
get2 (query)
dom.get2('div#test');
ready

Revisions

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