getElementsByClassName VS querySelectorAll

JavaScript performance comparison

Revision 61 of this test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div class='level1'>
 <div class='level2'>
    <ul class='level3'>
       <li><a href="#" name="name" class="level4">item 1</a></li>
    </ul>
  </div>
</div>
<script>
  var nodes;
</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
getElementsByClassName
var nodes = document.getElementsByClassName('level1')[0].getElementsByClassName('level2')[0].getElementsByClassName('level3')[0].getElementsByClassName('level4')[0];
var arr = [];
for (var i = 0, max = nodes.length; i < max; i++) arr.push(nodes[i]);
pending…
querySelectorAll
var nodes = document.querySelectorAll('.level1 .level2 .level3 .level4');
var arr = [];
for (var i = 0, max = nodes.length; i < max; i++) arr.push(nodes[i]);
pending…
jquery select
var nodes = $('.level1 .level2 .level3 .level4');
var arr = [];
for (var i = 0, max = nodes.length; i < max; i++) arr.push(nodes[i]);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment