querySelectorAll vs getElementsByTagName (v21)

Revision 21 of this benchmark created by Sebastian Poreba on


Preparation HTML

<div>
  <ul id="list" class="foo">
    <li><a href="#">item 1</a></li>
    <li><a href="#">item 2</a></li>
    <li><a href="#">item 3</a></li>
  </ul>
</div>

Test runner

Ready to run.

Testing in
TestOps/sec
querySelectorAll
var nodes = document.querySelectorAll('ul')
ready
getElementsByTagName
var nodes = document.getElementsByTagName('ul')
ready
getElementsByClassName
var nodes = document.getElementsByClassName('foo')
ready
getElementById
var nodes = document.getElementById('list')
ready
querySelectorAll class name
var nodes = document.querySelectorAll('.foo')
ready
querySelectorAll ID
var nodes = document.querySelectorAll('#list')
ready
querySelectorAll - real usage
var nodes = document.querySelectorAll('ul');
for (var i = 0; i < nodes.length; ++i) {
 nodes[i].className = "foo";
}
ready
getElementsByTagName - real usage
var nodes = document.getElementsByTagName('ul');
for (var i = 0; i < nodes.length; ++i) {
 nodes[i].className = "foo";
}
ready

Revisions

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