json parse/stringify vs manual recursion (v2)

Revision 2 of this benchmark created on


Setup

var sum = function(a,b){ return a+b; };
    var id = 0;
    
    var obj = {
       foo: sum,
       bar: [{
          baz: [["foo", "bar"], "baz", function(){}]
       }]
    };
    
    function recursive(data) {
        if (data && typeof data === "object")
            for (var p in data)
                if (typeof data[p] === "function")
                    data[p] = "func" + id++;
                else
                    recursive(data[p]);
        return data
    }
    
    function viaJSON(data){
        return JSON.parse(JSON.stringify(data, function (key, value) {
            if (typeof value === "function") {
                id++;
                return "func"+id;
            }
            return value;
        }));
    }

Test runner

Ready to run.

Testing in
TestOps/sec
recursive
recursive(obj);
ready
JSON
viaJSON(obj);
ready

Revisions

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