getElementById vs. querySelector

JavaScript performance comparison

Revision 248 of this test case created by

Preparation code

<p id="pp-0">Foo</p>
<p id="pp-1">Bar</p>
<p id="pp-2">Baz</p>
<p class="component-pp-0">Foo</p>
<p class="component-pp-1">Bar</p>
<p class="component-pp-2">Baz</p>
      
<script>
Benchmark.prototype.setup = function() {
  var id = "pp-1";

};
</script>

Preparation code output

<p id="pp-0">Foo</p> <p id="pp-1">Bar</p> <p id="pp-2">Baz</p> <p class="component-pp-0">Foo</p> <p class="component-pp-1">Bar</p> <p class="component-pp-2">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 bar = document.getElementById(id);
pending…
querySelector
var bar = document.querySelector(".component-" + id);
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

Foo

Bar

Baz