NodeList To Array

JavaScript performance comparison

Revision 24 of this test case created

Info

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

Preparation code

<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>
<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>
<script>
Benchmark.prototype.setup = function() {
    var nodeList = document.querySelectorAll('div');
    var cachedSlice = Array.prototype.slice;
    var slicer = function(arr) {
        return cachedSlice.call(arr);
        }
       
    var Slice = function(arr) {
        return Array.prototype.slice.call(arr)
        };
    var Arrayify = function(nodeList) {
        var outputArr = [],
            i = nodeList.length;
        while (i--) {
          outputArr[i] = nodeList[i]
        };
        return outputArr;
        }
       
    var ArrayForEach = function(nodeList) {
        var outputArr = [];
        Array.prototype.forEach.call(nodeList, function(node) {
          outputArr.push(node)
        });
        return outputArr;
        }
};
</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(nodeList);
pending…
push
var arr = [];
for (var i = 0, n; n = nodeList[i]; ++i) arr.push(n);
pending…
single for
var l = []; // Will hold the array of Node's
for (var i = 0, ll = nodeList.length; i != ll; l.push(nodeList[i++]));
pending…
count back
var arr = [];
for (var i = nodeList.length; i--; arr.unshift(nodeList[i]));
pending…
set length first
var arr = [];
for (var i = 0, ref = arr.length = nodeList.length >> 0; i < ref; i++) {
  arr[i] = nodeList[i];
}
pending…
set length first with Array constructor
var ref = nodeList.length,
    arr = new Array(ref);
for (var i = 0; i < ref; i++) {
  arr[i] = nodeList[i];
}
pending…
Set Length First, Terse
var a = [],
    l = nodeList.length >> 0;
for (; l--; a[l] = nodeList[l]);
pending…
Terse While
var a = [],
    l = nodeList.length >> 0;
while (l--) {
  a[l] = nodeList[l]
};
pending…
Slice Wrapper
var a = Slice(nodeList);
pending…
While as Function
var a = Arrayify(nodeList);
pending…
var arr = ArrayForEach(nodeList);
pending…
Terse While new Array
var l = nodeList.length,
    a = new Array(l);
while (l--) {
  a[l] = nodeList[l]
};
pending…
Cached Slice Wrapper
var a = slicer(nodeList);
pending…
Cached Slice
var a = cachedSlice.call(nodeList);
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