copy array slice-vs-concat (v14)

Revision 14 of this benchmark created on


Setup

var a = [];
  
  for (var i = 0; i < 10000; i += 1) {
    a.push({});
  }
  
  function validate(actual, expected) {
    if (actual.length !== expected.length) {
      throw new Error("actual length (" + actual.length + ") differs from expected length (" + expected.length + ")");
    }
    for (var i = 0; i < actual.length; i += 1) {
      if (actual[i] !== expected[i]) {
        throw new Error("actual value at " + i + " differs from expected value");
      }
    }
  }

Test runner

Ready to run.

Testing in
TestOps/sec
concat
var b = a.concat();
validate(b, a);
ready
while
var i = a.length;
var b = new Array(i);
while (i--) {
  b[i] = a[i];
}
validate(b, a);
ready
slice no arg
var b = a.slice();
validate(b, a);
ready
concat new
var b = [].concat(a);
validate(b, a);
ready
for loop
var len = a.length;
var b = new Array(len);
for(var i=0;i < len; i++) {
  b[i] = a[i];
}
validate(b, a);
ready
slice 1 arg
var b = a.slice(0);
validate(b, a);
ready
slice two arg
var b = a.slice(0, a.length);
validate(b, a);
ready
concat
var b = Array().concat(a);
validate(b, a);
ready
map
var b = a.map(function(g){return g;});
validate(b, a);
ready
filter
var b = a.filter(function(a){return true;});
validate(b, a);
ready
split()
var b = a.toString().split("");
validate(b, a);
ready
split() 2
var b = a.join().split("");
validate(b, a);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.