list all elements

JavaScript performance comparison

Test case created by epplestun

Preparation code

<div id="v">
<button id="b">Click</button>
{{test}}
    <div>
        <ul>
            <li><a href="#" data-click="click(1)">Click 1</a></li>
            <li><a href="#" data-click="click(2)">Click 2</a></li>
            <li><a href="#" data-click="click(3)">Click 3</a></li>
        </ul>
    </div>
</div>

Preparation code output

{{test}}

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
function getNodeList(elem) {
  var tags = [];
  for (i = 0; i < elem.all.length; i++) {
    tag = elem.all(i);
    tags.push(tag);
  }
  return tags;
}

getNodeList(document);
pending…
childNodes
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);
pending…
getElementsByTagName
function getNodeList(elem) {
  var all = elem.getElementsByTagName("*");
  var tags = [];
  for (var i = 0, max = all.length; i < max; i++) {
    tags.push(all[i]);
  }
}
getNodeList(document);
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