Clone NodeList (v2)

Revision 2 of this benchmark created by Denis G. on


Description

These tests aims to compare different methods to clone a nodelist into an array (comparable to array-clone).

Note: Some browser cannot use nodelist as apply arguments, others as issue keeping reference to live nodelist...

Preparation HTML

<div id='testdiv'></div>
<script>
  var div = document.getElementById('testdiv'),
      span = document.createElement('span'),
      slice = Array.prototype.slice,
      push = Array.prototype.push,
      unshift = Array.prototype.unshift,
      array, i, l, n, childs;
  
  for (var i = 0; i < 100; i++) {
   div.appendChild(span.cloneNode());
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
slice.call
array = slice.call(div.childNodes, 0);
ready
slice.apply
array = slice.apply(div.childNodes, [0]);
ready
push.apply
push.apply((array = []), div.childNodes);
ready
unshift.apply
unshift.apply((array = []), div.childNodes);
ready
Optimum increasing loop
childs = div.childNodes;
l = childs.length;
n = 0;
array = new Array(l);
for (i = -1; ++i < l;) {
 array[n++] = childs[i];
}
ready
Optimum decreasing loop
childs = div.childNodes;
n = (i = childs.length) - 1 >> 0;
array = new Array(i);
while (i--) {
 array[n--] = childs[i];
}
ready

Revisions

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

  • Revision 2: published by Denis G. on
  • Revision 7: published by Name64 on
  • Revision 8: published by Karl Seamon on