document.all vs document.getElementsByTagName

JavaScript performance comparison

Revision 4 of this test case created

Info

Speed test for getting all elements on the page

Preparation code

<div class="test1"></div>
<div class="test2"></div>
<div class="test3"></div>
<div class="test4"></div>
<div class="test5"></div>
<div class="test6"></div>
<div class="test7"></div>
<div class="test8"></div>
<div class="test9">
        <div class="test10"></div>
        <div class="test11"></div>
        <div class="test12"></div>
        <div class="test13"></div>
        <div class="test14"></div>
        <div class="test15"></div>
        <div class="test16"></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
document.all
var all = document.getElementsByTagName("*");
var elms = []
for (var i=0, max=all.length; i < max; i++) {
elms.push(all[i])
}
pending…
document.getElementsByTagName
function getNodeList(elem){
var l=new Array(elem),c=1,ret=new Array();
//This first loop will loop until the count var is stable//
for(var r=0;r<c;r++){
    //This loop will loop thru the child element list//
    for(var z=0;z<l[r].childNodes.length;z++){

         //Push the element to the return array.
        ret.push(l[r].childNodes[z]);

        if(l[r].childNodes[z].childNodes[0]){
            l.push(l[r].childNodes[z]);c++;
        }//IF          
    }//FOR
}//FOR
return ret;
}
getNodeList(document.body);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment