JavaScript Array Copy Comparison

JavaScript performance comparison

Test case created by JiangXiaoQiang

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    Object.deepCopy = function(obj) {
      if (typeof obj !== "object") return obj;
   
      var newObj = (obj instanceof Array) ? [] : {};
      for (i in obj) {
        if (obj[i] && typeof obj[i] == "object") newObj[i] = Object.clone(obj[i]);
        else newObj[i] = obj[i];
      }
      return newObj;
    };
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Copy by slice
var a = [1, 2, 3];
var b = a.slice(0);
pending…
Copy by splice
var a = [1, 2, 3];
var b = a.splice(0);
pending…
Copy by concat
var a = [1, 2, 3];
var b = a.concat();
pending…
Copy by stringify & eval
var a = [1, 2, 3];
var b = eval(JSON.stringify(a));
pending…
Copy by iterator
var a = [1, 2, 3];
var b = Object.deepCopy(a);
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