Deep Copy vs JSON Stringify / JSON Parse (v2)

Revision 2 of this benchmark created on


Setup

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;
    }
    
    function A(m,n){ 
     function row(k){ 
      var r = []; 
      for(var i=0;i<k;++i){r.push(Math.random()) }
      return r; 
     }
     var M = [];
     for(var j=0; j<m; ++j){ M.push(row(n)) }
     return M;
    }
    
    var M=A(100,100);

Test runner

Ready to run.

Testing in
TestOps/sec
Deep Copy
var bes = deepCopy(M)
ready
JSON Stringify / JSON Parse
var bes = JSON.parse(JSON.stringify(M))
ready

Revisions

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