getElementById.children vs. getElementsByClassName

JavaScript performance comparison

Revision 178 of this test case created by

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<p id="foo" class="my-foo">
  Foo
</p>
<p id="bar" class="my-bar">
  Bar
</p>
<div id="paa" class="my-paa">
  <div id="pbb" class="my-pbb">
    <div id="pcc" class="my-pcc">
      <div id="pdd" class="my-pdd">
        <div id="pee" class="my-pee">
          <div id="pff" class="my-pff">
            <span id="baz" class="my-baz">
              Baz
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
    

Preparation code output

<p id="foo" class="my-foo"> Foo </p> <p id="bar" class="my-bar"> Bar </p> <div id="paa" class="my-paa"> <div id="pbb" class="my-pbb"> <div id="pcc" class="my-pcc"> <div id="pdd" class="my-pdd"> <div id="pee" class="my-pee"> <div id="pff" class="my-pff"> <span id="baz" class="my-baz"> Baz </span> </div> </div> </div> </div> </div> </div>

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
getElementsByClassName (5 deep)
var pff = document.getElementsByClassName("my-pff")[0];
pending…
getElementById (5 deep)
var pff = document.getElementById("paa").children[0].children[0].children[0].children[0].children[0];
pending…
getElementsByClassName (4 deep)
var pee = document.getElementsByClassName("my-pee")[0];
pending…
getElementById (4 deep)
var pee = document.getElementById("paa").children[0].children[0].children[0].children[0];
pending…
getElementsByClassName (3 deep)
var pdd = document.getElementsByClassName("my-pdd")[0];
pending…
getElementById (3 deep)
var pdd = document.getElementById("paa").children[0].children[0].children[0];
pending…
getElementsByClassName (2 deep)
var pcc = document.getElementsByClassName("my-pcc")[0];
pending…
getElementById (0 deep)
var paa = document.getElementById("paa");
pending…
getElementsByClassName (0 deep)
var paa = document.getElementsByClassName("my-paa")[0];
pending…
getElementById (1 deep)
var pbb = document.getElementById("paa").children[0];
pending…
getElementsByClassName (1 deep)
var pbb = document.getElementsByClassName("my-pbb")[0];
pending…
getElementById (2 deep)
var pcc = document.getElementById("paa").children[0].children[0];
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