NodeList vs. array iteration

JavaScript performance comparison

Revision 16 of this test case created by Richard Kraaijenhagen

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(collection) {
   var result = [],
       i = -1;
   while (result[++i] = collection[i]) {};
   result.length--;
   return result;
  }
  
  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
for (var i = 0, length = liveList.length; i < length; i++) {
 liveList[i].nodeName;
}
pending…
Static List
for (var i = 0, length = statList.length; i < length; i++) {
 statList[i].nodeName;
}
pending…
Array of Live List
for (var i = 0, length = liveArr.length; i < length; i++) {
 liveArr[i].nodeName;
}
pending…
Array of Static List
for (var i = 0, length = statArr.length; i < length; i++) {
 statArr[i].nodeName;
}
pending…
Static list without caching length
for (var i = 0; i < statList.length; i++) {
 statList[i].nodeName;
}
pending…
Array of Static List without caching length
for (var i = 0; i < statArr.length; i++) {
 statArr[i].nodeName;
}
pending…
Static List with its iteration functions
var i=0;l=statList.getLength();
for(;i<l;i++) statList.item(i).nodeName;
pending…
Live List with its iteration functions
var i=0;l=liveList.getLength();
for(;i<l;i++) liveList.item(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.

1 Comment