getElementsByTagname vs querySelectorAll vs jQuery

JavaScript performance comparison

Revision 21 of this test case created by and last updated

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…
plain for
var fields = ['no','no','yes'], num = 0;
for (var i = 0; i < fields.length; i++) {
  if (fields[i] === 'yes') num+=1;
}
pending…
while check
var fields = ['no','no','yes'], len = fields.length, num = 0;
while (len--) {
  if (fields[len] === 'yes') num+=1;
}
pending…
regex for count
var fields = ['no','no','yes'], num = 0;
for (var i = 0; i < fields.length; i++) {
  if ((/yes/i).test(fields[i])) 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