array cloning (v103)

Revision 103 of this benchmark created by daweb on


Description

speed comparissons between slice, splice, for, Array, ...

add tests with: for(var i;a[i];i++){} instead of for (var i = 0, l = a.length; i < l; i++)

Setup

var a = [{
      test: 'bye'
    }, 'z', 'a', 'q', 'w', 's', 'x', 'c', 'd', 'v', 'e', 'f', 't', 'r', 'g', 't', 'm', 'y', 'h', 't', 'y', 'j', 'i', 'o', 'l', 'f', 's', 'd', 'j', 'n', 'm', 'c', 'u', 'i', 'e', 'w', 'h', 'r', ',', 'w', 'e', 'n', 'r', 'w', 'n', 'e', 'b', 'f', '2', '3', '4', '2', '3', '4', 'n', 'j', 'm', 'f', 'd', 's', 'j', 'd', 'y', 'f', 'g', '2', '3', 'h', 'f', 's', 'j', 'd', 'b', 'm', 'f', 'h', 's', 'd', 'h', 'f', 'o', '2', '3', 'i', 'z', 'a', 'q', 'w', 's', 'x', 'c', 'd', 'v', 'e', 'f', 't', 'r', 'g', 't', 'm', 'y', 'h', 't', 'y', 'j', 'i', 'o', 'l', 'f', 's', 'd', 'j', 'n', 'm', 'c', 'u', 'i', 'e', 'w', 'h', 'r', ',', 'w', 'e', 'n', 'r', 'w', 'n', 'e', 'b', 'f', '2', '3', '4', '2', '3', '4', 'n', 'j', 'm', 'f', 'd', 's', 'j', 'd', 'y', 'f', 'g', '2', '3', 'h', 'f', 's', 'j', 'd', 'b', 'm', 'f', 'h', 's', 'd', 'h', 'f', 'o', '2', '3', 'i', 'z', 'a', 'q', 'w', 's', 'x', 'c', 'd', 'v', 'e', 'f', 't', 'r', 'g', 't', 'm', 'y', 'h', 't', 'y', 'j', 'i', 'o', 'l', 'f', 's', 'd', 'j', 'n', 'm', 'c', 'u', 'i', 'e', 'w', 'h', 'r', ',', 'w', 'e', 'n', 'r', 'w', 'n', 'e', 'b', 'f', '2', '3', '4', '2', '3', '4', 'n', 'j', 'm', 'f', 'd', 's', 'j', 'd', 'y', 'f', 'g', '2', '3', 'h', 'f', 's', 'j', 'd', 'b', 'm', 'f', 'h', 's', 'd', 'h', 'f', 'o', '2', '3', 'i'];
    var b = [];

Test runner

Ready to run.

Testing in
TestOps/sec
slice
b = a.slice();
ready
concat
b = [].concat(a);
ready
for-unshift
b = [];
for (var i = a.length; i--;) {
  b.unshift(a[i]);
}
ready
for-push
b = [];
for (var i = 0, l = a.length; i < l; i++) {
  b.push(a[i]);
}
ready
for-index
b = [];
for (var i = 0, l = a.length; i < l; i++) {
  b[i] = a[i];
}
ready
while loop
var i = a.length;
while (i--) {
  b[i] = a[i];
}
ready
slice(0)
b = a.slice(0);
ready
a.concat()
b = a.concat()
ready
Array
b = Array.apply(undefined, a);
ready
splice
Array.prototype.splice.apply(b, [0, a.length].concat(a));
ready
spread
b = [...a];
ready
for-push - a[i]
b = [];
for (var i = 0; a[i]; i++) {
  b.push(a[i]);
}
ready
for-index - a[i]
b = [];
for (var i = 0; a[i]; i++) {
  b[i] = a[i];
}
ready
for-unshift - a[i]
b = [];
for (var i = 0; a[i]; i++) {
  b.unshift(a[i]);
}
ready

Revisions

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