querySelectorAll vs getElementsByTagName

JavaScript performance comparison

Revision 69 of this test case created by Dan

Preparation code

<div>
  <ul>
    <li><a href="#">item 1</a></li>
    <li><a href="#">item 2</a></li>
    <li><a href="#">item 3</a></li>
  </ul>
<a href="#"><span>Hello</span></a>
<strong>Bold!</strong>
</div>

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
querySelectorAll
var nodes = [];
var staticNodes = document.querySelectorAll('*');
for(var i = 0, length = staticNodes.length; i < length; ++i) {
    nodes.push(staticNodes[i]);
}
pending…
getElementsByTagName
var nodes = document.getElementsByTagName('*')
pending…
getElementsByTagName, converted to static list
var nodes = [];


if (!window.liveNodes) {
  window.liveNodes = document.getElementsByTagName('*');
}

var liveNodes = window.liveNodes;

for(var i = 0, length = liveNodes.length; i < length; ++i) {
    nodes.push(liveNodes[i]);
}
pending…
getElementsByTagName, converted to static list 2
//Seems to work, but the spec offers no guarantee of array functions working on NodeLists
var nodes = Array.prototype.slice.call(document.getElementsByTagName('*'));
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