array copy

JavaScript performance comparison

Revision 5 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr = new Array(1, 2, 3, 4, 5);
  
  function forinclone(arr) {
    var key, arr_clone = new Array(arr.length);
    for (key in arr) {
      arr_clone[key] = arr[key];
    }
    return arr_clone;
  }
  
  function forclone(arr) {
    var i, len = arr.length,
        arr_clone = new Array(len);
    for (i = 0; i < len; i += 1) {
      arr_clone[i] = arr[i];
    }
    return arr_clone;
  }
  
  function pushforclone(arr) {
    var i, len = arr.length,
        arr_clone = [];
    for (i = 0; i < len; i += 1) {
      arr_clone.push(arr[i]);
    }
    return arr_clone;
  }
  
  function whileclone(arr) {
    var len = arr.length,
        arr_clone = new Array(len);
    while (len--) {
      arr_clone[len] = arr[len];
    }
    return arr_clone;
  }

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
for-loop
arr_clone = forinclone(arr);
pending…
slice
arr_clone = arr.slice();
pending…
concat
arr_clone = arr.concat();
pending…
modified for-loop
arr_clone = forclone(arr);
pending…
push in for-loop
arr_clone = pushforclone(arr);
pending…
while-loop
arr_clone = whileclone(arr);
pending…
push
arr_clone = [];
arr_clone.push.apply(arr_clone, arr);
pending…
slice2
arr_clone = Array.prototype.slice.call(arr);
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.

0 Comments