NodeList To Array

JavaScript performance comparison

Revision 7 of this test case created by Eli Snow

Info

http://stackoverflow.com/questions/3199588/fastest-way-to-convert-javascript-nodelist-to-array

Preparation code

<div style="display:none">
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>
<div id="error_message" style="color:red; font-weight:bold;"></div>
<script>
  var nl = document.getElementsByTagName('*');
  try {
     if (Array.prototype.slice.call(nl).length !== nl.length)
        throw {};
  } catch(e) {
     document.getElementById("error_message").innerHTML = "Your browser does not support Array slicing against NodeLists. The result for slicing will be bogus."
  }
</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
Array.prototype.slice.call
var arr = Array.prototype.slice.call(nl);
pending…
push
var arr = [];
for (var i = 0, n; n = nl[i]; ++i) arr.push(n);
pending…
single for
var arr = [];
for (var i = 0, ref = nl.length; i < ref; arr[i] = nl[i++]);
pending…
count back
var arr = [];
for (var i = nl.length; i--; arr.unshift(nl[i]));
pending…
set length first
var arr = [];
for (var i = 0, ref = arr.length = nl.length; i < ref; arr[i] = nl[i++]);
pending…
unshift
var arr = [];
for (var i = arr.length = nl.length; i--; arr.unshift(nl[i]));
pending…
unshift2
var arr = new Array(nl.length);
for (var i = nl.length; i--; arr.unshift(nl[i]));
pending…
[].slice.call
var arr = [].slice.call(nl);
pending…
reverse while
var l = nl.length,
    arr = [];
while (l--) {
 arr[l] = nl[l];
}
pending…
[].push.apply
var arr = [];
arr.push.apply(arr, nl);
pending…
Reverse with preallocate
var i = nl.length,
    arr = new Array(i);
while (i--) arr[i] = nl[i];
pending…
Initialize array size
var arr = new Array(nl.length);
for (var i = 0, ref = nl.length; i < ref; arr[i] = nl[i++]);
pending…
push 2
var arr = new Array(nl.length);
for (var i = 0, n; n = nl[i]; ++i) arr.push(n);
pending…
[].push.apply 2
var arr = new Array(nl.length);
arr.push.apply(arr, nl);
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