Shallow Array Cloning (v98)

Revision 98 of this benchmark created by Ben White on


Description

speed comparisons between slice, splice, for, $.merge, Array

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

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;

Teardown


    if (b.length !== a.length) {
      throw {message: "Test failed! b.length !== a.length"};
    }
    b[0].test = 'hi';
    if (a[0].test !== 'hi') {
      throw {message: "Not shallow clone"};
    }
    for (var i = 1, l = a.length; i < l; i++) {
      if (b[i] !== a[i]) {
        throw {message: "Test failed! b array is " + b};
      }
    }
  

Test runner

Ready to run.

Testing in
TestOps/sec
slice()
b = a.slice();
 
ready
slice(0)
b = a.slice(0);
 
ready
concat
b = a.concat();
ready
concat(a)
b = [].concat(a);
 
ready
Array.apply
b = Array.apply(undefined, a);

 
ready
for-index
var l = a.length, i = 0;
b = new Array(l);
for (;i !== l; i++) {
  b[i] = a[i];
}
ready
for-index-reverse
var l = a.length;
b = new Array(l);
for (;l--;) {
  b[l] = a[l];
}
ready
for-index-literal
var l = a.length;
b = [];
for (;l--;) {
  b[l] = a[l];
}
ready
for-push
var l = a.length, i = 0;
b = [];
for (;i < l;i++) {
  b.push(a[i]);
}
ready
for-unshift
var l = a.length;
b = [];
for (;l--;) {
  b.unshift(a[l]);
}
ready
while
var l = a.length, i = 0;
b = new Array(l);
while (i !== l) {
  b[i] = a[i++];
}
ready
while-reverse
var l = a.length;
b = new Array(l);
while (l--) {
  b[l] = a[l];
}
ready
while-literal
var l = a.length;
b = [];
while (l--) {
  b[l] = a[l];
}
ready
filter
b = a.filter(function () { return true; });
ready
forEach
b = new Array(a.length);
a.forEach(function (v, i) { b[i] = v; });
ready

Revisions

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