getElementsByTagname vs querySelectorAll vs jQuery

JavaScript performance comparison

Revision 22 of this test case created by

Preparation code

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