querySelectorAll vs getElementsByTagName

JavaScript performance comparison

Revision 91 of this test case created

Preparation code

<script>
function $$c(x, y){
  var result = [];
  var count = 0;
  for(var i=0; i < x.length; i++){
    if(x[i].className == y){
      results[count++] = x[i];
    }
  }
  return(result);
}

function $unsetClass(x, y){
  for(var i=0; i < x.length; i++){
    x[i].className.replace(y,'');
  }
}
</script>
<div>
  <ul>
    <li><a href="#">item 1</a></li>
    <li><a href="#">item 2</a></li>
    <li><a class="xxx" 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.xxx')
pending…
getElementsByTagName
var nodes = document.getElementsByTagName('a');
$$c(nodes,"xxx");
pending…
var nod = document.getElementsByTagName('a');
$unsetClass(nod,"xxx");
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