getElementById vs. getElementsByClassName vs. querySelector vs. jQuery

JavaScript performance comparison

Revision 226 of this test case created by

Preparation code

<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-bar">
    <div id="paz" class="my-bar">
      <span id="baz" class="my-baz">
        Baz
      </span>
    </div>
  </div>
</div>
<div class="my-poo"></div>
<div class="my-poo"></div>
<div class="my-poo"></div>
<div class="my-poo"></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-bar"> <div id="paz" class="my-bar"> <span id="baz" class="my-baz"> Baz </span> </div> </div> </div> <div class="my-poo"></div> <div class="my-poo"></div> <div class="my-poo"></div> <div class="my-poo"></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 bar = document.getElementById("bar");
pending…
querySelector (ID)
var bar = document.querySelector("#bar");
pending…
jQuery(ID)
function find(el) {
 return document.getElementById(el)
}
find('bar')
pending…
getElementsByClassName
var bar = document.getElementsByClassName("my-poo");
pending…
querySelector (Class)
var bar = document.querySelectorAll(".my-poo");
pending…
func
function find(el) {
 return document.getElementsByClassName(el)
}
find('my-poo')
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