Archive | Java Script RSS feed for this section

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 + ")"  );
};

Tutuorials for Advanced JavaScript

17 Apr

These are the best tutorials out there, don’t miss these if you are/wannabe javascript programmer. ( Hold your nerves untill these videos load. )

PS: Douglas Crockford is the JavaScript architect of Yahoo!