querySelectorAll vs getElementsByTagName

JavaScript performance comparison

Revision 48 of this test case created by Adam

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>
</div>
<script>
Benchmark.prototype.setup = function() {
    var G = function(item) {  console.log(item);
      // if its an id alert
      if (item[0] === '#')
          return document.getElementById(item.slice(1));
      // if class alert
      else if (item[0] === '.')
          return document.getElementsByClassName(item.slice(1));
      else
         return document.getElementsByTagName(item);
    };
};
</script>

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')
pending…
getElementsByTagName
var nodes = document.getElementsByTagName('a')
pending…
var nodes = G('a')
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