querySelector vs querySelectorAll vs getElementsByClassName vs getElementsByTagName vs getElementsByName (v49)

Revision 49 of this benchmark created by tomByrer on


Preparation HTML

<section>
  <p id="foo" class="foo" name="fooname"><span class="bar" name="barname">Thanks <a href='http://mathiasbynens.be/'>Mathias</a> for jsperf!</span></p>
  <p id="baz" class="baz" name="bazname"><span class="bazbar" name="barname">:)</span></p>
  <p id="foo" class="foo" name="fooname"><span class="bar" name="barname">,,,</span></p>
</section>
<span class"clearfix"></span>
<section>
  <p id="foo" class="foo" name="fooname"><span class="bar" name="barname"><a href='https://www.youtube.com/watch?v=65-RbBwZQdU'>micro-benchmark talk by Vyacheslav Egorov</a> at LXJS 2013</span></p>
  <p id="baz" class="baz" name="bazname"><span class="bazbar" name="barname">;)</span></p>
  <p id="foo" class="foo" name="fooname"><span class="bar" name="barname">,,,</span></p>
</section>
<span class"clearfix"></span>
<section>
  <p id="foo" class="foo" name="fooname"><span class="bar" name="barname">Test edits by <a href='http://jsperf.com/browse/tombyrer'>tomByrer</a></span></p>
  <p id="baz" class="baz" name="bazname"><span class="bazbar" name="barname">X^)</span></p>
  <p id="foo" class="foo" name="fooname"><span class="bar" name="barname">,,,</span></p>
</section>

Test runner

Ready to run.

Testing in
TestOps/sec
qS by ID
document.querySelector("#foo");
ready
qSAll by ID
document.querySelectorAll("#foo")[0];
ready
gEByClassName
document.getElementsByClassName("foo")[0];
ready
gEByTagName
document.getElementsByTagName("p")[0];
ready
gEByName
document.getElementsByName("fooname")[0];
ready
qS by ClassName
document.querySelector(".foo");
ready
qSAll by ClassName
document.querySelectorAll(".foo")[0];
ready
qS by TagName
document.querySelector("p");
ready
qSAll by TagName
document.querySelectorAll("p")[0];
ready
qS by Name
document.querySelector("[name=fooname]");
ready
qSAll by Name
document.querySelectorAll("[name=fooname]")[0];
ready

Revisions

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