querySelectorAll vs getElementsByTagName

JavaScript performance comparison

Revision 77 of this test case created by D4Tocchini

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 list = [];
var nodes = document.querySelectorAll('*');

for(var i = 0; i < nodes.length; ++i) {
    list.push(nodes[i]);
}
pending…
byTagName
var nodes = document.getElementsByTagName('*');
var list = [];

for(var i = 0; i < nodes.length; ++i) {
    list.push(nodes[i]);
}
pending…
byTagName cached
var list = [];

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

var nodes = window.liveNodes;

for(var i = 0; i < nodes.length; ++i) {
    list.push(nodes[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