nodelist-vs-array-iteration

Benchmark created by John-David Dalton on


Preparation HTML

<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 live = toArray(document.getElementsByTagName('a'));
  var stat = toArray(document.querySelectorAll('a'));
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Array of Live List
for (var i = 0, length = live.length; i < length; i++) {
 live[i].nodeName;
}
ready
Array of Static List
for (var i = 0, length = stat.length; i < length; i++) {
 stat[i].nodeName;
}
ready

Revisions

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

  • Revision 1: published by John-David Dalton on