NodeList vs. array (slice) iteration

Benchmark created by Paul Grenier 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>
  var liveList = function() {
   return document.getElementsByTagName('a');
  },
      statList = function() {
    return document.querySelectorAll('a');
      },
      liveArr = function() {
    return Array.prototype.slice.apply(liveList());
      },
      statArr = function() {
    return Array.prototype.slice.apply(statList());
      };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Live List (while)
var l = liveList(),
    i = l.length;

while (i--) {
 l[i].nodeName;
}
ready
Static List (while)
var l = statList(),
    i = l.length;

while (i--) {
 l[i].nodeName;
}
ready
Array of Live List (while)
var l = liveArr(),
    i = l.length;

while (i--) {
 l[i].nodeName;
}
ready
Array of Static List (while)
var l = statArr(),
    i = l.length;

while (i--) {
 l[i].nodeName;
}
ready
Live List (for)
var l = liveList(),
    length = l.length;
for (var i = 0; i < length; i++) {
 l[i].nodeName;
}
ready
Static List (for)
var l = statList(),
    length = l.length;
for (var i = 0; i < length; i++) {
 l[i].nodeName;
}
ready
Array of Live List (for)
var l = liveArr(),
    length = l.length;
for (var i = 0; i < length; i++) {
 l[i].nodeName;
}
ready
Array of Static List (for)
var l = statArr(),
    length = l.length;
for (var i = 0; i < length; i++) {
 l[i].nodeName;
}
ready
Live List (while) (--i)
var l = liveList(),
    i = l.length;

while (--i) {
 l[i].nodeName;
}
ready
Static List (while) (--i)
var l = statList(),
    i = l.length;

while (--i) {
 l[i].nodeName;
}
ready
Array of Live List (while) (--i)
var l = liveArr(),
    i = l.length;

while (--i) {
 l[i].nodeName;
}
ready
Array of Static List (while) (--i)
var l = statArr(),
    i = l.length;

while (--i) {
 l[i].nodeName;
}
ready
Live List (for) (++i)
var l = liveList(),
    length = l.length,
    i = 0;
for (; i < length; ++i) {
 l[i].nodeName;
}
ready
Static List (for) (++i)
var l = statList(),
    length = l.length,
    i = 0;
for (; i < length; ++i) {
 l[i].nodeName;
}
ready
Array of Live List (for) (++i)
var l = liveArr(),
    length = l.length,
    i = 0;
for (; i < length; ++i) {
 l[i].nodeName;
}
ready
Array of Static List (for) (++i)
var l = statArr(),
    length = l.length,
    i = 0;
for (; i < length; ++i) {
 l[i].nodeName;
}
ready
Refined Live (for)1
var l = liveList(),
    length = l.length,
    i;
for (i = 0; i < length; ++i) {
 l[i].nodeName;
}
ready
Refined Live (for)2
var l = liveList(),
    length = l.length,
    i;
for (i = 0; i < length;) {
 l[i].nodeName;
 i++;
}
ready
Refined Live (for)3
var l = liveList(),
    length = l.length,
    i;
for (i = 0; i < length;) {
 l[i].nodeName;
 ++i;
}
ready
Live (while!!)1
var l = liveList(),
    i = l.length;
while ( !! i--) {
 l[i].nodeName;
}
ready
Live (while!!)2
var l = liveList(),
    i = l.length;
while ( !! --i) {
 l[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 Paul Grenier on