NodeList To Array

JavaScript performance comparison

Revision 25 of this test case created by

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,
      cachedPush = Array.prototype.push;
  
  var slicer = function(arr) {
      return cachedSlice.call(arr);
      }
  
  var pusher = function(nodes) {
    var arr = []
    cachedPush.apply(arr, nodes);
    return 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

<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>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
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…
Cached Push
var arr = [];
cachedPush.apply(arr, nodeList);
pending…
Cached Push Wrapper
var a = pusher(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.

0 Comments