querySelector id vs class vs element vs attribute version 2 (v65)

Revision 65 of this benchmark created on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
var i, $p = $('p');
for (i = 0; i < 500; i++)
$('<span></span>').attr("id", i).attr("class", i).attr("data-attribute", i).attr("name", i).appendTo($p);
</script>
<div>
  <p>
    <span id="id" class="class" data-attribute="attribute" name="name">
      Content
    </span>
  </p>
</div>

Setup

var el;

Test runner

Ready to run.

Testing in
TestOps/sec
queryselector id
document.querySelector("#id");
ready
queryselector class
document.querySelector(".class");
ready
querySelector attribute
document.querySelector("[data-attribute='attribute']");
ready
querySelector element
document.querySelector("span");
ready
querySelector element / class
document.querySelector("span.class");
ready
querySelector element / attribute
document.querySelector("span[data-attribute='attribute']");
ready
querySelector class / attribute
document.querySelector(".class[data-attribute='attribute']");
ready
querySelector element / class / attribute
document.querySelector("span.class[data-attribute='attribute']");
ready
querySelector attribute name
document.querySelector("[data-attribute]");
ready
querySelector element / id
document.querySelector("span#id");
ready
getElementsByClassName
document.getElementsByClassName("class")[0]
ready
getElementsByTagName
el = document.getElementsByTagName("span")[0]; 
el && (el.getAttribute("data-attribute") == 'attribute');
ready
getElementsByTagName matches
el = document.getElementsByTagName("span")[0]; 
el && el.matches("[data-attribute='attribute']");
ready
getElementsByName
document.getElementsByName("name")[0];
ready

Revisions

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