getElementsByTagname("a")[0] vs. querySelector("a") (v4)

Revision 4 of this benchmark created by Scott Sauyet on


Description

Tests how quickly you can iterate through multiple elements on the page using getElementsByTagName("a") vs. querySelector("a").

Preparation HTML

<ul>
  <li><a href="#">Item</a></li>
  <li><a href="#">Item</a></li>
  <li><a href="#">Item</a></li>
  <li><a href="#">Item</a></li>
  <li><a href="#">Item</a></li>
</ul>
<script>
  var list, result, i, len;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Using getElementsByTagName()
list = document.getElementsByTagName("a");
len = list.length;

for (i = 0; i < len; i++) {
 result = list[i];
}
ready
Using querySelector()
list = document.querySelector("a");
len = list.length;

for (i = 0; i < len; i++) {
 result = list[i];
}
ready

Revisions

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