getElementById vs. querySelector (v71)

Revision 71 of this benchmark created on


Description

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

Preparation HTML

<div>Padding for a more fair reflection of selection work being done</div>
<div>Padding for a more fair reflection of selection work being done</div>
<div>Padding for a more fair reflection of selection work being done</div><div>Padding for a more fair reflection of selection work being done</div>
<div>Padding for a more fair reflection of selection work being done</div>
<div>Padding for a more fair reflection of selection work being done</div>
<div>Padding for a more fair reflection of selection work being done</div>
<div>Padding for a more fair reflection of selection work being done</div>
<div>Padding for a more fair reflection of selection work being done</div>
<div id="find-me">
<div class="find-me"/>
<div class="find-me"/>
<div class="find-me"/>
</div>
<div>Padding for a more fair reflection of selection work being done</div>
<div>Padding for a more fair reflection of selection work being done</div>
<div>Padding for a more fair reflection of selection work being done</div>
<div>Padding for a more fair reflection of selection work being done</div>
<div>Padding for a more fair reflection of selection work being done</div>
<div>Padding for a more fair reflection of selection work being done</div>
<div>Padding for a more fair reflection of selection work being done</div>

Test runner

Ready to run.

Testing in
TestOps/sec
getElementById
var found = document.getElementById("find-me");
ready
getElementsByClassName
var found = document.getElementsByClassName(".find-me")[1];
ready
querySelector(id)
var found = document.querySelector("#find-me");
ready
querySelector(class)
var found = document.querySelector(".find-me")[1];
ready
querySelector(tag#id)
var found = document.querySelector("div#find-me");
ready
querySelector(tag.class)
var found = document.querySelector("div.find-me");
ready
getElementsByTagName
var found = document.getElementsByTagName("div")[0];
ready
querySelector(#id .class)
var found = document.querySelector("#find-me .find-me")[1];
ready
getElementById(id).getElementsByClassName(class)
var found = document.getElementById("find-me").getElementsByClassName("find-me")[1];
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.