querySelector id vs class vs element vs attribute version 2

JavaScript performance comparison

Revision 31 of this test case created

Preparation code

<script src="//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).appendTo($p);
</script>
<div>
  <p>
    <span id="id" class="class" data-attribute="attribute">
      Content
    </span>
  </p>
</div>

Preparation code output

Content

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
queryselector id
document.querySelector("#id");
pending…
queryselector class
document.querySelector(".class");
pending…
querySelector attribute
document.querySelector("[data-attribute='attribute']");
pending…
querySelector element
document.querySelector("span");
pending…
querySelector element / class
document.querySelector("span.class");
pending…
querySelector element / attribute
document.querySelector("span[data-attribute='attribute']");
pending…
querySelector class / attribute
document.querySelector(".class[data-attribute='attribute']");
pending…
querySelector element / class / attribute
document.querySelector("span.class[data-attribute='attribute']");
pending…
querySelector attribute name
document.querySelector("[data-attribute]");
pending…
getElementById
document.getElementById("id");
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