getElementsByTagname vs querySelectorAll vs jQuery

JavaScript performance comparison

Revision 18 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>
<input type="PASSWORD"></input><input type="PASSWORD"></input>
<input type="PASSWORD"></input><input type="PASSWORD"></input>
      
<script>
Benchmark.prototype.setup = function() {
  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> <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 querySelectorAll()
var fields = document.querySelectorAll("input[type=password]");
if (fields.length > 0) {
  hasFields = true;
}
pending…
Using querySelector()
var fields = document.querySelector("input[type=password]");
if (fields !== null) {
  hasFields = true;
}
pending…
jQuery
var fields = jQuery("input[type=password]");
if (fields.length > 0) {
  hasFields = true;
}
pending…
ByTagName while
var fields = document.getElementsByTagName("input");
var i = 0;
while (i < fields.length) {
  if (fields[i].type === "password") {
    hasFields = true; break;
  }
  i++;
}
pending…
ByTagName for
var fields = document.getElementsByTagName("input");
for (var i = 0; i < fields.length; i++) {
  if (fields[i].type === "password") {
    hasFields = true; break;
  }
}
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