Querying Elements

JavaScript performance comparison

Revision 2 of this test case created by

Preparation code

<div id="main">
<ul>
<li>Hello World</li>
<li>Hello World</li>
<li>Hello World</li>
<li>Hello World</li>
<li>Hello World</li>
</ul>
</div>
<script src="https://code.jquery.com/jquery-2.1.0.min.js"></script>
      
<script>
Benchmark.prototype.teardown = function() {
  var a = document.getElementById("main").getElementsByTagName("li");
  for(var i=0; i<a.length; i++){
  a[i].innerHTML = "Hello World";
  }

};
</script>

Preparation code output

<div id="main"> <ul> <li>Hello World</li> <li>Hello World</li> <li>Hello World</li> <li>Hello World</li> <li>Hello World</li> </ul> </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
getElementById+getElementsByTagName
var a = document.getElementById("main").getElementsByTagName("li");
for(var i=0; i<a.length; i++){
a[i].innerHTML = "Goodbye World";
}
pending…
querySelector+querySelectorAll
var a = document.querySelector("#main").querySelectorAll("li");
for(var i=0; i<a.length; i++){
a[i].innerHTML = "Goodbye World";
}
pending…
querySelectorAll
var a = document.querySelectorAll("#main li");
for(var i=0; i<a.length; i++){
a[i].innerHTML = "Goodbye World";
}
pending…
jQuery
$("#main li").html("Goodbye World");
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