Deep Copy vs JSON Stringify / JSON Parse

JavaScript performance comparison

Revision 7 of this test case created by fafsdfssad

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function deepCopy(o) {
      var copy = o,k;
   
      if (o && typeof o === 'object') {
          copy = Object.prototype.toString.call(o) === '[object Array]' ? [] : {};
          for (k in o) {
              copy[k] = deepCopy(o[k]);
          }
      }
   
      return copy;
  }
  
  var uc = {
    "left": 0,
    "top": 0,
    "width": 400,
    "height": 400,
    "borderLeft": 0,
    "borderTop": 0,
    "borderWidth": 400,
    "borderHeight": 400,
    "contentLeft": 0,
    "contentTop": 0,
    "contentWidth": 400,
    "contentHeight": 400,
    "paddingLeft": 0,
    "paddingTop": 0,
    "paddingWidth": 400,
    "paddingHeight": 400,
    "items": [
      {
        "left": 25,
        "top": 100,
        "width": 100,
        "height": 100,
        "borderLeft": 25,
        "borderTop": 100,
        "borderWidth": 100,
        "borderHeight": 100,
        "contentLeft": 25,
        "contentTop": 100,
        "contentWidth": 100,
        "contentHeight": 100,
        "paddingLeft": 25,
        "paddingTop": 100,
        "paddingWidth": 100,
        "paddingHeight": 100
      },
      {
        "left": 125,
        "top": 150,
        "width": 50,
        "height": 50,
        "borderLeft": 125,
        "borderTop": 150,
        "borderWidth": 50,
        "borderHeight": 50,
        "contentLeft": 125,
        "contentTop": 150,
        "contentWidth": 50,
        "contentHeight": 50,
        "paddingLeft": 125,
        "paddingTop": 150,
        "paddingWidth": 50,
        "paddingHeight": 50
      },
      {
        "left": 175,
        "top": 0,
        "width": 200,
        "height": 200,
        "borderLeft": 175,
        "borderTop": 0,
        "borderWidth": 200,
        "borderHeight": 200,
        "contentLeft": 175,
        "contentTop": 0,
        "contentWidth": 200,
        "contentHeight": 200,
        "paddingLeft": 175,
        "paddingTop": 0,
        "paddingWidth": 200,
        "paddingHeight": 200
      },
      {
        "left": 25,
        "top": 200,
        "width": 200,
        "height": 200,
        "borderLeft": 25,
        "borderTop": 200,
        "borderWidth": 200,
        "borderHeight": 200,
        "contentLeft": 25,
        "contentTop": 200,
        "contentWidth": 200,
        "contentHeight": 200,
        "paddingLeft": 25,
        "paddingTop": 200,
        "paddingWidth": 200,
        "paddingHeight": 200
      },
      {
        "left": 225,
        "top": 350,
        "width": 50,
        "height": 50,
        "borderLeft": 225,
        "borderTop": 350,
        "borderWidth": 50,
        "borderHeight": 50,
        "contentLeft": 225,
        "contentTop": 350,
        "contentWidth": 50,
        "contentHeight": 50,
        "paddingLeft": 225,
        "paddingTop": 350,
        "paddingWidth": 50,
        "paddingHeight": 50
      },
      {
        "left": 275,
        "top": 300,
        "width": 100,
        "height": 100,
        "borderLeft": 275,
        "borderTop": 300,
        "borderWidth": 100,
        "borderHeight": 100,
        "contentLeft": 275,
        "contentTop": 300,
        "contentWidth": 100,
        "contentHeight": 100,
        "paddingLeft": 275,
        "paddingTop": 300,
        "paddingWidth": 100,
        "paddingHeight": 100
      }
    ]
  };

};
</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
Deep Copy
var bes = deepCopy(uc)
pending…
JSON Stringify / JSON Parse
var bes = JSON.parse(JSON.stringify(uc))
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