I working on a web application that receives JSON data with uppercase property names. I need those property names to be lowercase, so I'm using a function to loop recursively through the JSON object and convert them to lowercase.
The problem is my JSON replies can get very large. I want the function to perform well even if it has to process JSON with 60,000 property names and various levels of nesting.
The lowercasing function is:
FN = function (obj)
{var ret = null;
if (typeof(obj) == "string" || typeof(obj) == "number")
return obj;
else if (obj.push)
ret = [];
else
ret = {};
for (var key in obj)
ret[String(key).toLowerCase()] = FN(obj[key]);
return ret;
};
And I'm performing some benchmarking here: http://jsfiddle.net/emw89/7/
The above test clocks in at ~570ms on my machine.
Is there anything I can do to improve the performance of this function?
Edit: I closed my IE, re-opened IE and ran the jsfiddle benchmark again--it's now coming in at ~180ms for me. My IE had been open for a couple days straight until just then, so maybe that's what was causing such poor performance. Either way, I'm still interested if there's a way to optimize this function further. Any time extra time spent processing JSON directly adds to the elapsed time of every AJAX request.
I would do it with the simple regex replace.
e.g.
I Hope this helps.
If you are processing large JSON objects with IE (or any other browser - but IE is most likely to spit the dummy) I recommend processing your json/array in chunks.
Good explanation of that process can be found here http://oreilly.com/server-administration/excerpts/even-faster-websites/writing-efficient-javascript.html (scroll down to the "Timer Patterns for Yielding" heading)
100% faster.