getElementById vs. getElementsByClassName vs. querySelector vs. jQuery

JavaScript performance comparison

Revision 31 of this test case created by

Preparation code

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

Preparation code output

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

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