NodeList To Array

JavaScript performance comparison

Revision 4 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>
<script>
  var nl = document.getElementsByTagName('*');
</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
for (var i = 0, ref = nl.length, arr = []; 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
for (var i = 0, arr = [], 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…

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