getElementsByClassName vs regex

JavaScript performance comparison

Test case created by John Cockrell

Preparation code

<div id="test">
   <div class="child-1"></div>
   <div class="child-2"></div>
   <div class="child-3"></div>
   <div class="child-4"></div>
   <div class="child-5"></div>
</div>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
getElementsByClassName
document.getElementsByClassName('test-3')[0];
pending…
regex
var children = document.getElementById('test'),
    i = 0,
    len = children.length,
    c_i;

for (; i < len;  i += 1) {
    c_i = children[i];
    if (/test\-3/.test(c_i)) return c_i;
}
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment