copy array slice-vs-concat (v16)

Revision 16 of this benchmark created on


Setup

var a = [];
  
  var a_len = 30;
  for (var i = 0; i < a_len; 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_len;
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
for loop
var b = new Array(a_len);
for(var i = 0; i < a_len; ++i)
  b[i] = a[i];
validate(b, a);
ready

Revisions

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