querySelector id vs class vs element vs attribute version 2

JavaScript performance comparison

Revision 65 of this test case created by

Preparation code

<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>
      
<script>
Benchmark.prototype.setup = function() {
  var el;

};
</script>

Preparation code output

<div> <p> <span id="id" class="class" data-attribute="attribute" name="name"> Content </span> </p> </div>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
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…
querySelector element / id
document.querySelector("span#id");
pending…
getElementsByClassName
document.getElementsByClassName("class")[0]
pending…
getElementsByTagName
el = document.getElementsByTagName("span")[0]; 
el && (el.getAttribute("data-attribute") == 'attribute');
pending…
getElementsByTagName matches
el = document.getElementsByTagName("span")[0]; 
el && el.matches("[data-attribute='attribute']");
pending…
getElementsByName
document.getElementsByName("name")[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.

0 Comments

Content