querySelectorAll vs getElementsByTagName

JavaScript performance comparison

Revision 22 of this test case created by MONTILLET

Preparation code

<div id="test">
  <ul>
    <li>
      <a href="#">1</a>
    </li>
    <li>
      <a href="#">2</a>
    </li>
    <li>
      <a href="#">3</a>
    </li>
  </ul>
</div>
<script>
  var slice = Array.prototype.slice;
 
  function toArray(a) {
    return slice.call(a);
  }
 
  var div = document.getElementById('test');
 
  function test(a) {
    if (a.map(function(e) {
      return e.innerHTML;
    }).join('') !== '123') {
      throw new Error('Wrong result!');
    }
  }
</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
test(toArray(div.querySelectorAll('a')))
pending…
getElementsByTagName
test(toArray(div.getElementsByTagName('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