getElementById vs. getElementsByClassName vs. querySelector vs. jQuery

JavaScript performance comparison

Revision 86 of this test case created by

Preparation code

<script src="https://code.jquery.com/jquery-git2.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-bar">
    <div id="paz" class="my-bar">
      <span id="baz" class="my-baz">
        Baz
      </span>
      <span id="badumtss" class="m">
        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-bar"> <div id="paz" class="my-bar"> <span id="baz" class="my-baz"> Baz </span> <span id="badumtss" class="m"> 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 then className
var poo = document.getElementById("poo").getElementsByClassName("my-bar");
pending…
getElementById
var bar = document.getElementById("bar");
pending…
querySelector (ID)
var bar = document.querySelector("#bar");
pending…
jQuery separate ID's
var bar = $("#bar");
var poo = $("#poo");
pending…
getElementsByClassName
var bar = document.getElementsByClassName("my-bar");
pending…
getElementById qSA Context
var poo = document.getElementById("poo").querySelectorAll(".my-bar");
pending…
jQuery Wrapped Selector
var bar = $(document.getElementById("poo").querySelectorAll(".my-bar"));
pending…
short getElementsByClassName
var bar = document.getElementsByClassName("m");
pending…
querySelectorAll combined
var bar = document.querySelectorAll("#poo .my-bar")
pending…
jQuery .add
var bar = $("#bar").add("#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 Baz