getElementById vs. querySelector

JavaScript performance comparison

Revision 9 of this test case created

Info

Compare the speed of getElementById to querySelector

Preparation code

<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo</p>

<p id="foo">Foo</p>
</p>

<p>Foo</p>
<p>Bar</p>
<p>Foo</p>
<p>Bar</p>
<p>Foo</p>
<p>Bar</p>
<p>Foo</p>
<p>Bar</p>
<p>Foo</p>
<p>Bar</p>

<p>Foo</p>
<p>Bar</p>
<p>Baz</p>
<p>Foo</p>
<p>Foo</p>
<p>Foo

<p>Foo</p>
<p>Bar</p>
<p>Baz</p>
<p>Foo</p>
<p>Bar
<p id="bar">Bar</p>
</p>

</p>


<p>Baz</p>
<p>Foo</p>
<p>Bar</p>
<p>Baz</p>
<p>Foo</p>
<p>Bar</p>
<p>Baz</p>
<p>Foo</p>
<p>Bar</p>
<p>Baz</p>
<p>Foo</p>
<p>Bar</p>
<p>Baz</p>
<p>Foo</p>
<p>Bar</p>
<p>Baz</p>
<p>Foo</p>
<p>Bar</p>
<p>Baz</p>
<p>Foo</p>
<p>Bar</p>
<p>Baz</p>
<p id="baz">Baz</p>
<p>Foo</p>
<p>Bar</p>
<p>Baz</p>

<p>Foo</p>
<p>Bar</p>
<p>Baz</p>
<p>Foo</p>
<p>Bar</p>
<p>Baz</p>
<p>Foo</p>
<p>Bar</p>
<p>Baz</p>
<p>Foo</p>
<p>Bar</p>
<p>Baz</p>
<p>Foo</p>
<p>Bar</p>
<p>Baz</p>/p

Preparation code output

Foo

Foo

Foo

Foo

Foo

Foo

Foo

Foo

Foo

Foo

Foo

Foo

Foo

Foo

Bar

Foo

Bar

Foo

Bar

Foo

Bar

Foo

Bar

Foo

Bar

Baz

Foo

Foo

Foo

Foo

Bar

Baz

Foo

Bar

Bar

Baz

Foo

Bar

Baz

Foo

Bar

Baz

Foo

Bar

Baz

Foo

Bar

Baz

Foo

Bar

Baz

Foo

Bar

Baz

Foo

Bar

Baz

Baz

Foo

Bar

Baz

Foo

Bar

Baz

Foo

Bar

Baz

Foo

Bar

Baz

Foo

Bar

Baz

Foo

Bar

Baz

/p

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
getElementById
var bar = document.getElementById("bar");
pending…
querySelector
var bar = document.querySelector("#bar");
pending…
jQuery Test
var bar = jQuery('#bar');
pending…
$(querySelector)
var bar = $(document.querySelector("#bar"));
pending…
$(querySelectorAll)
var bar = $(document.querySelectorAll("#bar"));
pending…
$(getElementById)
var bar = $(document.getElementById("bar"));
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. Here’s a list of current revisions for this page:

0 comments

Add a comment