NodeList vs. array (slice) iteration

JavaScript performance comparison

Test case created by Paul Grenier

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>
  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>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

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

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

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

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

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

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

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

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

while (--i) {
 l[i].nodeName;
}
pending…
Live List (for) (++i)
var l = liveList(),
    length = l.length,
    i = 0;
for (; i < length; ++i) {
 l[i].nodeName;
}
pending…
Static List (for) (++i)
var l = statList(),
    length = l.length,
    i = 0;
for (; i < length; ++i) {
 l[i].nodeName;
}
pending…
Array of Live List (for) (++i)
var l = liveArr(),
    length = l.length,
    i = 0;
for (; i < length; ++i) {
 l[i].nodeName;
}
pending…
Array of Static List (for) (++i)
var l = statArr(),
    length = l.length,
    i = 0;
for (; i < length; ++i) {
 l[i].nodeName;
}
pending…
Refined Live (for)1
var l = liveList(),
    length = l.length,
    i;
for (i = 0; i < length; ++i) {
 l[i].nodeName;
}
pending…
Refined Live (for)2
var l = liveList(),
    length = l.length,
    i;
for (i = 0; i < length;) {
 l[i].nodeName;
 i++;
}
pending…
Refined Live (for)3
var l = liveList(),
    length = l.length,
    i;
for (i = 0; i < length;) {
 l[i].nodeName;
 ++i;
}
pending…
Live (while!!)1
var l = liveList(),
    i = l.length;
while ( !! i--) {
 l[i].nodeName;
}
pending…
Live (while!!)2
var l = liveList(),
    i = l.length;
while ( !! --i) {
 l[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. Here’s a list of current revisions for this page:

0 comments

Add a comment