getElementById vs. getElementsByClassName vs. querySelector vs. jQuery vs xpath

JavaScript performance comparison

Revision 48 of this test case created by

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<p id="bar" class="my-baz">
  Bar
</p>
<span id="baz" class="my-baz">
  Baz
</span>

    

Preparation code output

<p id="bar" class="my-baz"> Bar </p> <span id="baz" class="my-baz"> Baz </span>

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
getElementById
var bar = document.getElementById("bar");
pending…
querySelector (ID)
var bar = document.querySelector("#bar");
pending…
jQuery ID Selector
var bar = $("#bar");
pending…
xpath eval
var bar = document.evaluate("//*[@id='bar']", document, null, 9, null).singleNodeValue;
pending…
getElementsByClassName
var baz = document.getElementsByClassName("my-baz");
pending…
querySelector (Class)
var baz = document.querySelector(".my-baz");
pending…
jQuery ClassSelector
var baz = $(".my-baz");
pending…
jQuery w. querySelector
var baz = $(document.querySelector(".my-baz"));
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

Bar

Baz