array cloning

JavaScript performance comparison

Revision 31 of this test case created

Info

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

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
 
<script>
Benchmark.prototype.setup = function() {
    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 = [];
};

Benchmark.prototype.teardown = function() {
    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 a deep clone"};
    }
    for (var i = 1, l = a.length; i < l; i++) {
      if (b[i] !== a[i]) {
        throw {
          message: "Test failed! b array is " + b
        };
      }
    }
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
slice
b = a.slice();
pending…
concat
b = [].concat(a);
pending…
for-unshift
b = [];
for (var i = a.length; i--;) {
  b.unshift(a[i]);
}
pending…
for-push
b = [];
for (var i = 0, l = a.length; i < l; i++) {
  b.push(a[i]);
}
pending…
for-index
b = new Array(a.length);
for (var i = 0, l = a.length; i < l; i++)
  b[i] = a[i];
pending…
while loop
var i = a.length;
while (i--) b[i] = a[i];
pending…
slice(0)
b = a.slice(0);
pending…
a.concat()
b = a.concat()
pending…
Array
b = Array.apply(undefined, a);
pending…
Map
b = a.map(function(i) {
  return i;
});
pending…
Array.prototype.push.apply
b = [];
Array.prototype.push.apply(b, a);
pending…
Array.prototype.splice.apply
b = [];
a.unshift(0, 0);
Array.prototype.splice.apply(b, a);
a.splice(0, 2);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment