NodeList To Array

JavaScript performance comparison

Revision 29 of this test case created

Info

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

Preparation code

<style>noder{display:none;}</style>
<script>
for(var i=10000;i--;document.body.appendChild(document.createElement('noder')));

var nl = document.getElementsByTagName('noder');
</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 l = []; // Will hold the array of Node's
for (var i = 0, ll = nl.length; i != ll; l.push(nl[i++]));
pending…
set length first
var arr = [];
for (var i = 0, ref = arr.length = nl.length; i < ref; i++) {
 arr[i] = nl[i];
}
pending…
set length first 2
var arr = [],
  len = nl.length,
  i;

for (i = 0; i < len; i += 1) {
 arr[i] = nl[i];
}
pending…
Terse While new Array
var l=nl.length, arr = new Array(l);
while(l--){arr[l]=nl[l]};
pending…
Array.unshift
// nl is the nodelist
var arr = [];
for (var i = nl.length; i--; arr.unshift(nl[i]));
pending…
Bitwise operation
var arr = [];
for (var i=0, l=nl.length >>> 0; i++ !==l; arr[i] = nl[i]);
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