array cloning (v25)

Revision 25 of this benchmark created by dan on


Setup

var a = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0];
    var b1 = a.concat([1, 2]);
    var b2 = a.concat([{a: 1, b: 'a'}, {c: 'dsfase', d: {a: 1, x: 2}}]);

Teardown


    if (c.length !== b1.length || c.length !== b2.length) {
      throw {message: "Test failed! b.length !== c.length"};
    }
    
    for (var i = 0, l = c.length; i < l; i++) {
    // we compare with || before l - 2 and with && afterwards
      if (i < l - 2)
        if (b1[i] !== c[i] || b2[i] !== c[i]) {
          throw {message: "Test failed! Arrays' content not equal on index  " + i};
        }
      else
        if (b1[i] !== c[i] && b2[i] !== c[i]) {
          throw {message: "Test failed! Arrays' content not equal on index  " + i};
        }
    }
    
    c[0].test = 'hi';
    if (b1[0].test === 'hi' || b2[0].test === 'hi') {
      throw {message: "Not a deep clone"};
    }
    
  

Test runner

Ready to run.

Testing in
TestOps/sec
slice, clean
c = b1.slice();
 
ready
slice, dirty
c = b2.slice();
 
ready
concat, clean
c = b1.concat();
 
ready
concat, dirty
c = b2.concat();
 
ready
while loop clean
//for (var i = 0, l = a.length; i < l; i++) {
//  c[i] = b1[i];
//}
var c = [];
var i = b1.length;
while(i--) { c[i] = b1[i]; }
 
ready
while loop dirty
var c = [];
var i = b2.length;
while(i--) { c[i] = b2[i]; }
 
ready
slice(0) clean
c = b1.slice(0);
 
ready
slice(0) dirty
c = b2.slice(0);
 
ready

Revisions

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