getElementById vs. getElementsByClassName vs. querySelector vs. jQuery

JavaScript performance comparison

Revision 231 of this test case created by sgraham785

Preparation code

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

Preparation code output

<p id="foo" class="my-foo"> Foo </p> <p id="bar" class="my-bar"> Bar </p> <div id="poo" class="my-poo"> <div id="par" class="my-par"> <div id="paz" class="my-paz"> <span id="baz" class="my-baz"> Baz </span> </div> </div> </div>

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 foo = document.getElementById("foo");
var bar = document.getElementById("bar");
var poo = document.getElementById("poo");
var par = document.getElementById("par");
var paz = document.getElementById("paz");
var baz = document.getElementById("baz");
pending…
querySelector (ID)
var foo = document.querySelector("#foo");
var bar = document.querySelector("#bar");
var poo = document.querySelector("#poo");
var par = document.querySelector("#par");
var paz = document.querySelector("#paz");
var baz = document.querySelector("#baz");
pending…
jQuery ID Selector
var foo = $("#foo");
var bar = $("#bar");
var poo = $("#poo");
var par = $("#par");
var paz = $("#paz");
var baz = $("#baz");
pending…
getElementsByClassName
var foo = document.getElementsByClassName("my-foo");
var bar = document.getElementsByClassName("my-bar");
var poo = document.getElementsByClassName("my-poo");
var par = document.getElementsByClassName("my-par");
var paz = document.getElementsByClassName("my-paz");
var baz = document.getElementsByClassName("my-baz");
pending…
querySelector (Class)
var foo = document.querySelector("my-foo");
var bar = document.querySelector("my-bar");
var poo = document.querySelector("my-poo");
var par = document.querySelector("my-par");
var paz = document.querySelector("my-paz");
var baz = document.querySelector("my-baz");
pending…
jQuery Class Selector
var foo = $("my-foo");
var bar = $("my-bar");
var poo = $("my-poo");
var par = $("my-par");
var paz = $("my-paz");
var baz = $("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

Foo

Bar

Baz