Evaluating JSON Data
14 Dec
Evaluating JSON data is the most fundamental and common use-case for any modern ajax based web application. The most commonly used method to evaluate json strings has always been eval() but there are few other methodologies that I have learnt recently.
1. eval()
var jsonData = window["eval"]( "(" + jsonString + ")" );Support: All Browsers variants
2. new Function()
var jsonData = new Function( " return ( " + jsonString + " ); " );
Support: All Browsers variants
3. Native JSON Object
var jsonData = JSON.parse( jsonString );
Support: IE8, FF3
Performance benchmarks for these JSON evaluation methods proves that
Performance of Native JSON Object > eval() > new Function
PS: new Function > eval() for the gekho browsers
So, we could create a wrapper to make sure JSON data is evaluated efficiently.
function parseJSONData( jsonString ){
if(JSON !== 'undefined' ){
return JSON.parse( jsonString );
}
if(AjxEnv.isGekho){
return new Function( "return ( "+jsonString+ " ); " );
}
return window['eval']( "(" + jsonString + ")" );
};
Is there any way to compress and pass the json object? Since, I have huge amount of data getting into object it is kicking the performance. If you have any clue plz help me.