querySelectorAll vs getElementsByTagName

JavaScript performance comparison

Revision 76 of this test case created by attr vs class

Preparation code

<div>
  <ul>
    <li><a href="#">item 1</a></li>
    <li><a class="foo" href="#">item 2</a></li>
    <li><a href="#">item 3</a></li>
  </ul>
</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 = document.querySelectorAll('a.foo')
pending…
getElementsByTagName
var result = document.getElementsByTagName('a'),
    nodes = [];
for (var i = 0, len = result.length; i < len; i++) {
  if (result[i].className.indexOf('foo') !== -1) {
    nodes.push(result[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