getElementsByTagname vs querySelectorAll vs jQuery

JavaScript performance comparison

Revision 20 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>
      
<script>
Benchmark.prototype.setup = function() {
  var hasFields = false;
  var num = 0;

};

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

};
</script>

Preparation code output

<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…
for
var fields = document.getElementsByTagName('input');
for (var i = 0; i < fields.length; i++) {
  if (fields[i].type === 'password') {
    hasFields = true;
  }
}
pending…
regex for
var fields = document.getElementsByTagName('input');
for (var i = 0; i < fields.length; i++) {
  if ((/password/i).test(fields[i].type)) {
    hasFields = true;
  }
}
pending…
regex for count
var fields = document.getElementsByTagName('input'), num = 0;
for (var i = 0; i < fields.length; i++) {
  if ((/password/i).test(fields[i].type)) num++;
}
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