querySelector vs querySelectorAll vs getElementById vs getElementsByClassName vs getElementsByTagName vs getElementsByName (v71)

Revision 71 of this benchmark created on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<section>
  <p id="foo" class="foo" name="fooname"></p>
  <span class="bar" name="barname">,,,</span>
  <div>
    <ul>
      <li>
        <a href="#" name="name" class="multipleInstances">item 1</a>
      </li>
      <li>
        <a href="#" name="name" class="multipleInstances">item 2</a>
      </li>
      <li>
        <a href="#" name="name" class="multipleInstances">item 3</a>
      </li>
      <li>
        <a href="#" name="name" class="multipleInstances">item 4</a>
      </li>
      <li>
        <a href="#" name="name" class="multipleInstances">item 5</a>
      </li>
      <li>
        <a href="#" name="name" class="multipleInstances">item 6</a>
      </li>
      <li>
        <a href="#" name="name" class="multipleInstances">item 7</a>
      </li>
      <li>
        <a href="#" name="name" class="multipleInstances">item 8</a>
      </li>
      <li>
        <a href="#" name="name" class="multipleInstances">item 9</a>
      </li>
      <li>
        <a href="#" name="name" class="multipleInstances">item 10</a>
      </li>
    </ul>
  </div>
</section>

Test runner

Ready to run.

Testing in
TestOps/sec
querySelector by ID
document.querySelector("#foo");
ready
querySelectorAll by ID
document.querySelectorAll("#foo")[0];
ready
getElementById
document.getElementById("foo");
ready
getElementsByClassName
document.getElementsByClassName("foo")[0];
ready
getElementsByTagName
document.getElementsByTagName("p")[0];
ready
getElementsByName
document.getElementsByName("fooname")[0];
ready
querySelector by ClassName
document.querySelector(".foo");
ready
querySelectorAll by ClassName
document.querySelectorAll(".foo")[0];
ready
querySelector by TagName
document.querySelector("p");
ready
querySelectorAll by TagName
document.querySelectorAll("p")[0];
ready
querySelector by Name
document.querySelector("[name=fooname]");
ready
querySelectorAll by Name
document.querySelectorAll("[name=fooname]")[0];
ready
multiple querySelectorAll
document.querySelectorAll(".multipleInstances");
ready
multiple className
document.getElementsByClassName("multipleInstances");
ready
multiple first querySelector
document.querySelector(".multipleInstances");
ready
multiple first className
document.getElementsByClassName("multipleInstances")[0];
ready
multiple tagName
document.getElementsByTagName("LI");
ready
multiple children
var list = document.getElementsByTagName("UL")[0],
kids = list.children;
ready

Revisions

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