getElementById vs. querySelector vs customSelector

JavaScript performance comparison

Revision 169 of this test case created by

Preparation code

<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<p id="foo">Foo</p>
<p id="j_id0:SiteTemplate:j_id10:j_id174:viewEditIncident:readMode:ViewEditIncidentViewButtonsTop:ViewEditIncidentViewEditButtonTop">Bar</p>
<p id="baz">Baz</p>
      
<script>
Benchmark.prototype.setup = function() {
  var customSelector = function customSelector(selector) {
  return document.getElementById(selector) || document.querySelector(selector);
  }

};
</script>

Preparation code output

<p id="foo">Foo</p> <p id="j_id0:SiteTemplate:j_id10:j_id174:viewEditIncident:readMode:ViewEditIncidentViewButtonsTop:ViewEditIncidentViewEditButtonTop">Bar</p> <p id="baz">Baz</p>

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
getElementById
var foo = document.getElementById("j_id0:SiteTemplate:j_id10:j_id174:viewEditIncident:readMode:ViewEditIncidentViewButtonsTop:ViewEditIncidentViewEditButtonTop");
pending…
querySelector
var bar = document.querySelector('#j_id0\\:SiteTemplate\\:j_id10\\:j_id174\\:viewEditIncident\\:readMode\\:ViewEditIncidentViewButtonsTop\\:ViewEditIncidentViewEditButtonTop');
pending…
select(ID)
var baz = customSelector('j_id0:SiteTemplate:j_id10:j_id174:viewEditIncident:readMode:ViewEditIncidentViewButtonsTop:ViewEditIncidentViewEditButtonTop');
pending…
select(custom)
var bang = customSelector('#j_id0\\:SiteTemplate\\:j_id10\\:j_id174\\:viewEditIncident\\:readMode\\:ViewEditIncidentViewButtonsTop\\:ViewEditIncidentViewEditButtonTop'); // forces querySelector return
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

Foo

Bar

Baz