array cloning (v111)

Revision 111 of this benchmark created on


Description

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

DO NOT EVER reverse iterate (if you do not care about deleting elements), the new addition while loop++ is ( on firefox ) faster than anything on chrome or firefox.

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 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 = Array(a.length);
for (var i = 0, l = a.length; i < l; i++) {
  b[i] = a[i];
}
ready
while loop
var i = a.length,
  b = Array(i);
while (--i >= 0) {
  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
Map Identity
b = a.map(function(x) {
  return x;
});
ready
Map Deep Clone
b = a.map(function(x) {
  var y;
  if (typeof x === 'object') {
    y = $.extend({}, x);
  } else {
    y = x;
  }
  return y;
});
ready
while loop++
var len = a.length,
  i = -1,
  b = Array(len);
while (++i < len) {
  b[i] = a[i];
}
ready
Object.create(fake clone)
var b = Object.create(a);
ready
Object.assign(ES6)
var b = Array(a.length);
Object.assign(b, a);
ready

Revisions

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