querySelectorAll vs getElementsByTagName (v22)

Revision 22 of this benchmark created by MONTILLET on


Preparation HTML

<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>

Test runner

Ready to run.

Testing in
TestOps/sec
querySelectorAll
test(toArray(div.querySelectorAll('a')))
ready
getElementsByTagName
test(toArray(div.getElementsByTagName('a')))
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.