querySelector vs querySelectorAll vs getElementById vs getElementsByClassName vs getElementsByTagName vs $

JavaScript performance comparison

Revision 39 of this test case created by Owain Llewellyn

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<p id="foo" class="foo">
  bar
</p>

Preparation code output

bar

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
document.querySelector("#foo");
pending…
querySelectorAll
document.querySelectorAll("#foo")[0];
pending…
getElementById
document.getElementById("foo");
pending…
getElementsByClassName
document.getElementsByClassName("foo")[0];
pending…
getElementsByTagName
document.getElementsByTagName("p");
pending…
jQuery - Element
$('p')
pending…
jQuery - Class
$('p.foo')
pending…
jQuery - Id
$('#foo')
pending…
jQuery
$('.foo')
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