Access to nodes via querySelectorAll vs getElementsByTagName

JavaScript performance comparison

Revision 2 of this test case created by

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>
    <li><a href="#">item 4</a></li>
    <li><a href="#">item 5</a></li>
    <li><a href="#">item 6</a></li>
    <li><a href="#">item 7</a></li>
    <li><a href="#">item 8</a></li>
    <li><a href="#">item 9</a></li>
    <li><a href="#">item 10</a></li>
    <li><a href="#">item 11</a></li>
    <li><a href="#">item 12</a></li>
    <li><a href="#">item 13</a></li>
    <li><a href="#">item 14</a></li>
    <li><a href="#">item 15</a></li>
    <li><a href="#">item 16</a></li>
    <li><a href="#">item 17</a></li>
  </ul>
</div>
    

Preparation code output

<div> <ul> <li><a href="#">item 1</a></li> <li><a href="#">item 2</a></li> <li><a href="#">item 3</a></li> <li><a href="#">item 4</a></li> <li><a href="#">item 5</a></li> <li><a href="#">item 6</a></li> <li><a href="#">item 7</a></li> <li><a href="#">item 8</a></li> <li><a href="#">item 9</a></li> <li><a href="#">item 10</a></li> <li><a href="#">item 11</a></li> <li><a href="#">item 12</a></li> <li><a href="#">item 13</a></li> <li><a href="#">item 14</a></li> <li><a href="#">item 15</a></li> <li><a href="#">item 16</a></li> <li><a href="#">item 17</a></li> </ul> </div>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
querySelectorAll
var nodes = document.querySelectorAll('a');
var href;
for (var i = 0, iLength = nodes.length; i < iLength; i++) {
 href = nodes[i].href;
 nodes[i].href = 'another url';
}
pending…
getElementsByTagName
var nodes = document.getElementsByTagName('a');
for (var i = 0, iLength = nodes.length; i < iLength; i++) {
 href = nodes[i].href;
 nodes[i].href = 'another url';
}
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.

0 Comments