NodeList vs. array iteration

JavaScript performance comparison

Revision 9 of this test case created by

Preparation code

<div>
  <ul>
    <li>
      <a href="#">item 1</a>
    </li>
    <li>
      <a href="#">item 2</a>
    </li>
    <li>
      <a href="#">item 3</a>
    </li>
  </ul>
</div>
<script>
  function toArray(nl) {
    var l = [],
        i = 0,
        ln = nl.length;
    for (; i != ln; l[i] = nl[i++]);
    return l;
  }

  var liveList = document.getElementsByTagName('a');
  var statList = document.querySelectorAll('a');
  var liveArr = toArray(liveList);
  var statArr = toArray(statList);
</script>
    

Preparation code output

<div> <ul> <li> <a href="#">item 1</a> </li> <li> <a href="#">item 2</a> </li> <li> <a href="#">item 3</a> </li> </ul> </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
Live List
var length = liveList.length,
    i = 0;
for (; i < length; i++) {
  liveList[i].nodeName;
}
pending…
Static List
var length = statList.length,
    i = 0;
for (; i < length; i++) {
  statList[i].nodeName;
}
pending…
Array of Live List
var length = liveArr.length,
    i = 0;
for (; i < length; i++) {
  liveArr[i].nodeName;
}
pending…
Array of Static List
var length = statArr.length,
    i = 0;
for (; i < length; i++) {
  statArr[i].nodeName;
}
pending…
Static List to Array
var staticArr = toArray(statList),
    length = staticArr.length,
    i = 0;
for (; i < length; i++) {
  staticArr[i].nodeName;
}
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