querySelector vs querySelectorAll vs getElementById vs getElementsByClassName vs getElementsByTagName vs getElementsByName

JavaScript performance comparison

Revision 35 of this test case created by hooriza

Preparation code

<section>
  <p id="foo" class="foo" name="fooname"></p>
  <span class="bar" name="barname">,,,</span>
</section>

Preparation code output

,,,

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
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…

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. Here’s a list of current revisions for this page:

0 comments

Add a comment