jQuery .class selector

JavaScript performance comparison

Test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>


<div id="container"><div><div><div class="template"><div class="template"></div></div></div></div></div>
<script>
Benchmark.prototype.setup = function() {
    function getFirst(elem) {
    var divs = elem.getElementsByTagName('div'),
                i, len;
   
        for (i = 0, len = divs.length; i < len; i+=1) {
                if (divs[i].className === 'template') {
                        return divs[i];
                }
        }
   
        return null;
    }
   
    function getFirst2(elem) {
    return elem.getElementsByClassName('template')[0];
    }
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Test 1
var div = document.getElementById('container');
var template = $('.template', div).first();
pending…
Test 2
var div = document.getElementById('container');
var template = $(div).find('.template').first();
pending…
Test 3
var div = document.getElementById('container');
var template = getFirst(div);

 
pending…
test 4
var div = document.getElementById('container');
var template = getFirst2(div);
pending…

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

Compare results of other browsers

0 comments

Add a comment