querySelector vs querySelectorAll vs getElementById vs getElementsByClassName vs getElementsByTagName vs getElementsByName

JavaScript performance comparison

Revision 71 of this test case created by

Preparation code

<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>
    

Preparation code output

<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

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
querySelector by ID
document.querySelector("#foo");
pending…
querySelectorAll by ID
document.querySelectorAll("#foo")[0];
pending…
getElementById
document.getElementById("foo");
pending…
getElementsByClassName
document.getElementsByClassName("foo")[0];
pending…
getElementsByTagName
document.getElementsByTagName("p")[0];
pending…
getElementsByName
document.getElementsByName("fooname")[0];
pending…
querySelector by ClassName
document.querySelector(".foo");
pending…
querySelectorAll by ClassName
document.querySelectorAll(".foo")[0];
pending…
querySelector by TagName
document.querySelector("p");
pending…
querySelectorAll by TagName
document.querySelectorAll("p")[0];
pending…
querySelector by Name
document.querySelector("[name=fooname]");
pending…
querySelectorAll by Name
document.querySelectorAll("[name=fooname]")[0];
pending…
multiple querySelectorAll
document.querySelectorAll(".multipleInstances");
pending…
multiple className
document.getElementsByClassName("multipleInstances");
pending…
multiple first querySelector
document.querySelector(".multipleInstances");
pending…
multiple first className
document.getElementsByClassName("multipleInstances")[0];
pending…
multiple tagName
document.getElementsByTagName("LI");
pending…
multiple children
var list = document.getElementsByTagName("UL")[0],
kids = list.children;
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

,,,