getElementsByTagname("a")[0] vs. querySelector("a")

JavaScript performance comparison

Revision 17 of this test case created by

Preparation code

<script src="https://code.jquery.com/jquery-git2.min.js"></script>

<input type="password"></input><input type="password"></input>
<input type="password"></input><input type="password"></input>
      
<script>
Benchmark.prototype.setup = function() {
  var list, result, i, len;
  var hasFields = false;

};

Benchmark.prototype.teardown = function() {
  hasFields = false;

};
</script>

Preparation code output

<input type="password"></input><input type="password"></input> <input type="password"></input><input type="password"></input>

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
Using getElementsByTagName()
var fields = document.getElementsByTagName("input");

if (fields && fields.length > 0) {
  hasFields = true;
}
pending…
Using querySelectorAll()
var fields = document.querySelectorAll("input[type='password']");

if (fields && fields.length > 0) {
  hasFields = true;
}
pending…
Using querySelector()
var fields = document.querySelector("input[type='password']");

if (fields && fields.length > 0) {
  hasFields = true;
}
pending…
jQuery
fields = jQuery("input[type=password]");

if (fields && fields.length > 0) {
  hasFields = true;
}
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