getElementById vs. querySelector

JavaScript performance comparison

Revision 70 of this test case created

Info

Compare the speed of the multitude of ways to select a node in JavaScript.

Preparation code

<div id="find-me" class="find-me">Find me!</div>

Preparation code output

Find me!

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 found = document.getElementById("find-me");
pending…
getElementsByClassName
var found = document.getElementsByClassName(".find-me")[0];
pending…
querySelector(id)
var found = document.querySelector("#find-me");
pending…
querySelector(class)
var found = document.querySelector(".find-me");
pending…
querySelector(tag#id)
var found = document.querySelector("div#find-me");
pending…
querySelector(tag.class)
var found = document.querySelector("div.find-me");
pending…
getElementsByTagName
var found = document.getElementsByTagName("div")[0];
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