Deep Copy vs JSON Stringify / JSON Parse

JavaScript performance comparison

Revision 94 of this test case created by Jonathan Perry

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function deepCopyArrayReduce (array) {
      return array.reduce((a, c) => a.concat(deepCopyReduce(c)), [])
  }
  
  function deepCopyObjectReduce (object) {
      const r = {};
      for (k in object) {
          const c = object[k];
          r[k] = deepCopyReduce(c);
      }
      return r;    
  }
  
  function deepCopyArrayForIn (array) {
      const r = [];
      for (k in array) {
          const c = array[k];
          r[k] = deepCopyForIn(c);
      }
      return r;    
  }
  
  function deepCopyObjectForIn (object) {
      const r = {};
      for (k in object) {
          const c = object[k];
          r[k] = deepCopyForIn(c);
      }
      return r;    
  }
  
  function deepCopyArrayFor (array) {
      let i = 0;
      const n = array.length;
      const r = [];
      for (i, n; i < n; i = i + 1) {
          const c = array[i];
          r.push(deepCopyFor(c));
      }
      return r;    
  }
  
  function deepCopyObjectFor (object) {
      const r = {};
      for (k in object) {
          const c = object[k];
          r[k] = deepCopyFor(c);
      }
      return r;    
  }
  
  function deepCopyReduce(v) {
      if (Array.isArray(v)) return deepCopyArrayReduce(v);
      if ((v || false) instanceof Object) return deepCopyObjectReduce(v)
      return v;
  }
  
  function deepCopyForIn(v) {
      if (Array.isArray(v)) return deepCopyArrayForIn(v);
      if ((v || false) instanceof Object) return deepCopyObjectForIn(v)
      return v;
  }
  
  function deepCopyFor(v) {
      if (Array.isArray(v)) return deepCopyArrayFor(v);
      if ((v || false) instanceof Object) return deepCopyObjectFor(v)
      return v;
  }
  
  function deepCopyInlineFor(v) {
      if (Array.isArray(v)) {
          let i = 0;
          const n = array.length;
          const r = [];
          for (i, n; i < n; i = i + 1) {
              const c = array[i];
              r.push(deepCopyInlineFor(c));
          }
          return r;  
      } 
  
      if ((v || false) instanceof Object) {
          const r = {};
          for (k in v) {
              const c = v[k];
              r[k] = deepCopyInlineFor(c);
          }
          return r;    
      }
  
      return v;
  }
  
  function deepCopyInlineForIn(v) {
      if (Array.isArray(v)) {
          const r = [];
          for (k in v) {
              const c = v[k];
              r[k] = deepCopyInlineForIn(c);
          }
          return r;    
      } 
  
      if ((v || false) instanceof Object) {
          const r = {};
          for (k in v) {
              const c = v[k];
              r[k] = deepCopyInlineForIn(c);
          }
          return r;    
      }
  
      return v;
  }
  
  var uc = {bir:{iki:{uc:3}}}

};
</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 Inline For
var bes = deepCopyInlineFor(uc)
pending…
Deep Copy Reduce
var bes = deepCopyReduce(uc)
pending…
Deep Copy For ... In
var bes = deepCopyForIn(uc)
pending…
Deep Copy For
var bes = deepCopyFor(uc)
pending…
JSON Stringify / JSON Parse
var bes = JSON.parse(JSON.stringify(uc))
pending…
Deep Copy Inline For ... In
var bes = deepCopyInlineForIn(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