array cloning (v115)

Revision 115 of this benchmark created by whitie on


Description

speed comparissons 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'];
    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 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};
      }
    }
  

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
slice(0)
b = a.slice(0);
 
ready
a.concat()
b = a.concat()
ready
Array
b = Array.apply(undefined,a);
 
ready
while loop, pre-allocated
b = new Array(a.length);
var i = a.length;
while(i--) { b[i] = a[i]; }
ready
while backward
b = [];
var i = a.length;
while(i--) { b[i] = a[i]; }
ready
for backward
var n = a.length
b = []
b.length = n
for(var i=n-1;i>=0;i--) b[i] = a[i]
ready
for forward , last before
var n = a.length-1
b = []
b.length = n+1 ; b[n]=a[n]
for(var i=0;i<n;i++) b[i] = a[i]
ready
while forward claused
var n = a.length, i=-1
b = []
while(++i<n) b[i] = a[i]
ready
w/forward length preinformed
var n = a.length, i=-1
b = [] ; b.length=n
while(++i<n) b[i] = a[i]
ready

Revisions

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