querySelectorAll vs getElementsByTagName vs Hybrid (v30)

Revision 30 of this benchmark created by Philip Tellis on


Description

Compare document.querySelectorAll vs document.getElementsByTagName vs a hybrid approach that uses the one fastest in the given browser.

Looks at all elements and pulls out its length to force getElementsByTagName to dereference the live nodeList.

Preparation HTML

<div>
  <ul>
    <li><a href="#">item 1</a></li>
    <li><a href="#">item 2</a></li>
    <li><a href="#">item 3</a></li>
  </ul>
</div>
<form>
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
</form>
<div>
<p>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
</p>
<p>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
<strong><em><span><a href="#"></a></span></em></strong>
</p>
</div>

Setup

var f=(/Opera/.test(navigator.userAgent)?document.querySelectorAll:document.getElementsByTagName);

Test runner

Ready to run.

Testing in
TestOps/sec
querySelectorAll
var nodes = document.querySelectorAll('*'),
nodelength = nodes.length;
ready
getElementsByTagName
var nodes = document.getElementsByTagName('*'),
nodelength = nodes.length;
ready
hybrid
var nodes = f.call(document, '*'),
nodelength = nodes.length;
ready

Revisions

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