getElementsByClassName VS querySelectorAll

JavaScript performance comparison

Revision 229 of this test case created by

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div>
  <ul>
    <li><a href="#" name="name" class="one">item 1</a></li>
    <li><a href="#" name="name" class="two">item 2</a></li>
    <li><a href="#" name="name" class="classname">item 3</a></li>
    <li><a href="#" name="name" class="three">item 4</a></li>
    <li><a href="#" name="name" class="classname">item 5</a></li>
    <li><a href="#" name="name" class="classname">item 6</a></li>
    <li><a href="#" name="name" class="five">item 7</a></li>
    <li><a href="#" name="name" class="classname">item 8</a></li>
    <li><a href="#" name="name" class="classname">item 9</a></li>
    <li><a href="#" name="name" class="nine">item 10</a>
  <ul>
    <li><a href="#" name="name" class="three">item 1</a></li>
    <li><a href="#" name="name" class="classname">item 2</a></li>
    <li><a href="#" name="name" class="five">item 3</a></li>
    <li><a href="#" name="name" class="classname two three">item 4</a></li>
    <li><a href="#" name="name" class="classname two three">item 5</a></li>
    <li><a href="#" name="name" class="classname">item 6</a></li>
    <li><a href="#" name="name" class="nine">item 7</a></li>
    <li><a href="#" name="name" class="classname">item 8</a></li>
    <li><a href="#" name="name" class="classname">item 9</a></li>
    <li><a href="#" name="name" class="classname">item 10</a>
  <ul>
    <li><a href="#" name="name" class="classname two three">item 1</a></li>
    <li><a href="#" name="name" class="classname">item 2</a></li>
    <li><a href="#" name="name" class="three">item 3</a></li>
    <li><a href="#" name="name" class="classname">item 4</a></li>
    <li><a href="#" name="name" class="one">item 5</a></li>
    <li><a href="#" name="name" class="classname">item 6</a></li>
    <li><a href="#" name="name" class="classname">item 7</a></li>
    <li><a href="#" name="name" class="classname two three">item 8</a></li>
    <li><a href="#" name="name" class="classname">item 9</a></li>
    <li><a href="#" name="name" class="five">item 10</a>  <ul>
    <li><a href="#" name="name" class="classname">item 1</a></li>
    <li><a href="#" name="name" class="classname">item 2</a></li>
    <li><a href="#" name="name" class="classname">item 3</a></li>
    <li><a href="#" name="name" class="classname two three">item 4</a></li>
    <li><a href="#" name="name" class="classname">item 5</a></li>
    <li><a href="#" name="name" class="eight">item 6</a></li>
    <li><a href="#" name="name" class="classname">item 7</a></li>
    <li><a href="#" name="name" class="classname">item 8</a></li>
    <li><a href="#" name="name" class="classname">item 9</a></li>
    <li><a href="#" name="name" class="nine">item 10</a>  <ul>
    <li><a href="#" name="name" class="classname">item 1</a></li>
    <li><a href="#" name="name" class="classname">item 2</a></li>
    <li><a href="#" name="name" class="classname two three">item 3</a></li>
    <li><a href="#" name="name" class="classname">item 4</a></li>
    <li><a href="#" name="name" class="classname">item 5</a></li>
    <li><a href="#" name="name" class="classname two three">item 6</a></li>
    <li><a href="#" name="name" class="test">item 7</a></li>
    <li><a href="#" name="name" class="classname">item 8</a></li>
    <li><a href="#" name="name" class="no">item 9</a></li>
    <li><a href="#" name="name" class="classname">item 10</a>  <ul>
    <li><a href="#" name="name" class="classname">item 1</a></li>
    <li><a href="#" name="name" class="classname">item 2</a></li>
    <li><a href="#" name="name" class="classname">item 3</a></li>
    <li><a href="#" name="name" class="classname">item 4</a></li>
    <li><a href="#" name="name" class="classname two three">item 5</a></li>
    <li><a href="#" name="name" class="classname">item 6</a></li>
    <li><a href="#" name="name" class="test">item 7</a></li>
    <li><a href="#" name="name" class="classname">item 8</a></li>
    <li><a href="#" name="name" class="three">item 9</a></li>
    <li><a href="#" name="name" class="classsname">item 10</a>  <ul>
    <li><a href="#" name="name" class="classname">item 1</a></li>
    <li><a href="#" name="name" class="classname">item 2</a></li>
    <li><a href="#" name="name" class="test">item 3</a></li>
    <li><a href="#" name="name" class="classname">item 4</a></li>
    <li><a href="#" name="name" class="classname">item 5</a></li>
    <li><a href="#" name="name" class="classname two three">item 6</a></li>
    <li><a href="#" name="name" class="nine">item 7</a></li>
    <li><a href="#" name="name" class="classname">item 8</a></li>
    <li><a href="#" name="name" class="classname">item 9</a></li>
    <li><a href="#" name="name" class="three">item 10</a></li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul>  <ul>
    <li><a href="#" name="name" class="one">item 1</a></li>
    <li><a href="#" name="name" class="classname two three">item 2</a></li>
    <li><a href="#" name="name" class="classname">item 3</a></li>
    <li><a href="#" name="name" class="classname two three">item 4</a></li>
    <li><a href="#" name="name" class="classname two three">item 5</a></li>
    <li><a href="#" name="name" class="classname">item 6</a></li>
    <li><a href="#" name="name" class="two">item 7</a></li>
    <li><a href="#" name="name" class="classname">item 8</a></li>
    <li><a href="#" name="name" class="classname">item 9</a></li>
    <li><a href="#" name="name" class="classname two three">item 10</a></li>
  </ul></li>
  </ul></li>
  </ul>
    </li>
  </ul>
</div>
<script>
  var nodes;
</script>
    

Preparation code output

<div> <ul> <li><a href="#" name="name" class="one">item 1</a></li> <li><a href="#" name="name" class="two">item 2</a></li> <li><a href="#" name="name" class="classname">item 3</a></li> <li><a href="#" name="name" class="three">item 4</a></li> <li><a href="#" name="name" class="classname">item 5</a></li> <li><a href="#" name="name" class="classname">item 6</a></li> <li><a href="#" name="name" class="five">item 7</a></li> <li><a href="#" name="name" class="classname">item 8</a></li> <li><a href="#" name="name" class="classname">item 9</a></li> <li><a href="#" name="name" class="nine">item 10</a> <ul> <li><a href="#" name="name" class="three">item 1</a></li> <li><a href="#" name="name" class="classname">item 2</a></li> <li><a href="#" name="name" class="five">item 3</a></li> <li><a href="#" name="name" class="classname two three">item 4</a></li> <li><a href="#" name="name" class="classname two three">item 5</a></li> <li><a href="#" name="name" class="classname">item 6</a></li> <li><a href="#" name="name" class="nine">item 7</a></li> <li><a href="#" name="name" class="classname">item 8</a></li> <li><a href="#" name="name" class="classname">item 9</a></li> <li><a href="#" name="name" class="classname">item 10</a> <ul> <li><a href="#" name="name" class="classname two three">item 1</a></li> <li><a href="#" name="name" class="classname">item 2</a></li> <li><a href="#" name="name" class="three">item 3</a></li> <li><a href="#" name="name" class="classname">item 4</a></li> <li><a href="#" name="name" class="one">item 5</a></li> <li><a href="#" name="name" class="classname">item 6</a></li> <li><a href="#" name="name" class="classname">item 7</a></li> <li><a href="#" name="name" class="classname two three">item 8</a></li> <li><a href="#" name="name" class="classname">item 9</a></li> <li><a href="#" name="name" class="five">item 10</a> <ul> <li><a href="#" name="name" class="classname">item 1</a></li> <li><a href="#" name="name" class="classname">item 2</a></li> <li><a href="#" name="name" class="classname">item 3</a></li> <li><a href="#" name="name" class="classname two three">item 4</a></li> <li><a href="#" name="name" class="classname">item 5</a></li> <li><a href="#" name="name" class="eight">item 6</a></li> <li><a href="#" name="name" class="classname">item 7</a></li> <li><a href="#" name="name" class="classname">item 8</a></li> <li><a href="#" name="name" class="classname">item 9</a></li> <li><a href="#" name="name" class="nine">item 10</a> <ul> <li><a href="#" name="name" class="classname">item 1</a></li> <li><a href="#" name="name" class="classname">item 2</a></li> <li><a href="#" name="name" class="classname two three">item 3</a></li> <li><a href="#" name="name" class="classname">item 4</a></li> <li><a href="#" name="name" class="classname">item 5</a></li> <li><a href="#" name="name" class="classname two three">item 6</a></li> <li><a href="#" name="name" class="test">item 7</a></li> <li><a href="#" name="name" class="classname">item 8</a></li> <li><a href="#" name="name" class="no">item 9</a></li> <li><a href="#" name="name" class="classname">item 10</a> <ul> <li><a href="#" name="name" class="classname">item 1</a></li> <li><a href="#" name="name" class="classname">item 2</a></li> <li><a href="#" name="name" class="classname">item 3</a></li> <li><a href="#" name="name" class="classname">item 4</a></li> <li><a href="#" name="name" class="classname two three">item 5</a></li> <li><a href="#" name="name" class="classname">item 6</a></li> <li><a href="#" name="name" class="test">item 7</a></li> <li><a href="#" name="name" class="classname">item 8</a></li> <li><a href="#" name="name" class="three">item 9</a></li> <li><a href="#" name="name" class="classsname">item 10</a> <ul> <li><a href="#" name="name" class="classname">item 1</a></li> <li><a href="#" name="name" class="classname">item 2</a></li> <li><a href="#" name="name" class="test">item 3</a></li> <li><a href="#" name="name" class="classname">item 4</a></li> <li><a href="#" name="name" class="classname">item 5</a></li> <li><a href="#" name="name" class="classname two three">item 6</a></li> <li><a href="#" name="name" class="nine">item 7</a></li> <li><a href="#" name="name" class="classname">item 8</a></li> <li><a href="#" name="name" class="classname">item 9</a></li> <li><a href="#" name="name" class="three">item 10</a></li> </ul></li> </ul></li> </ul></li> </ul> <ul> <li><a href="#" name="name" class="one">item 1</a></li> <li><a href="#" name="name" class="classname two three">item 2</a></li> <li><a href="#" name="name" class="classname">item 3</a></li> <li><a href="#" name="name" class="classname two three">item 4</a></li> <li><a href="#" name="name" class="classname two three">item 5</a></li> <li><a href="#" name="name" class="classname">item 6</a></li> <li><a href="#" name="name" class="two">item 7</a></li> <li><a href="#" name="name" class="classname">item 8</a></li> <li><a href="#" name="name" class="classname">item 9</a></li> <li><a href="#" name="name" class="classname two three">item 10</a></li> </ul></li> </ul></li> </ul> </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('.classname.two.three');
var arr = [];
for (var i = 0, max = nodes.length; i < max; i++) arr.push(nodes[i]);
pending…
getElementsByClassName
var nodes = document.getElementsByClassName('classname');
var arr = [];
for (var i = 0, max = nodes.length; i < max; i++) arr.push(nodes[i]);
pending…
jquery select
var nodes = $('.classname.two.three');
var arr = [];
for (var i = 0, max = nodes.length; i < max; i++) arr.push(nodes[i]);
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