querySelectorAll vs getElementsByTagName (v111)

Revision 111 of this benchmark created by Andy Herron on


Description

Accessed all elements of the array, since getElementsByTagName is a "live list" and doesn't actually calculate anything.

The previous tests are all skewed by this (although gEBTN is still a lot faster).

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div>
  <ul>
    <li><a href="#">item 1</a></li>
    <li><a href="#">item 2</a></li>
    <li><a href="#">item 3</a></li>
    <li><a href="#">item 4</a></li>
    <li><a href="#">item 5</a></li>
    <li><a href="#">item 6</a></li>
    <li><a href="#">item 7</a></li>
    <li><a href="#">item 8</a></li>
    <li><a href="#">item 9</a></li>
    <li><a href="#">item 10</a></li>
  </ul>
</div>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery $(tag)
var nodes = $('a');
var length = nodes.length;
var node1 = nodes[0];
var node2 = nodes[1];
var node3 = nodes[2];
var node4 = nodes[3];
var node5 = nodes[4];
var node6 = nodes[5];
var node7 = nodes[6];
var node8 = nodes[7];
var node9 = nodes[8];
var node10 = nodes[9];
ready
querySelectorAll
var nodes = document.querySelectorAll('a');
var length = nodes.length;
var node1 = nodes[0];
var node2 = nodes[1];
var node3 = nodes[2];
var node4 = nodes[3];
var node5 = nodes[4];
var node6 = nodes[5];
var node7 = nodes[6];
var node8 = nodes[7];
var node9 = nodes[8];
var node10 = nodes[9];
ready
getElementsByTagName
var nodes = document.getElementsByTagName('a');
var length = nodes.length;
var node1 = nodes[0];
var node2 = nodes[1];
var node3 = nodes[2];
var node4 = nodes[3];
var node5 = nodes[4];
var node6 = nodes[5];
var node7 = nodes[6];
var node8 = nodes[7];
var node9 = nodes[8];
var node10 = nodes[9];
ready

Revisions

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