Loop vs Slice copy (v7)

Revision 7 of this benchmark created on


Setup

var data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Test runner

Ready to run.

Testing in
TestOps/sec
loop copy
var copy = []
for (var j = 0; j < data.length; j++) {
  copy.push(data[j]);
}
ready
splice copy
var copy = data.slice(0);
ready
loop fixed size
var copy = new Array(data.length);
for (var j = 0; j < data.length; j++) {
  copy[j] = data[j];
}
ready
array 2 string 2 array
var x = data.join(",")
var copy = x.split(",")
ready
while
var i = 0,
    len = data.length,
    copy = [];
while (i < len) {
  copy[i] = data[i];
  i++;
}
ready

Revisions

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