Dynamic Websites and JSON

Mar 5, 2010 at 2:46 PM
Edited Mar 5, 2010 at 2:46 PM

For the past year or so I have been writing a CMS engine that allows for dynamic template generation.  And it works quite nicely.  All the widgets that a user chooses to display on a template are saved as a JSON configuration string against the template, and then when a template is used, a copy of this string is saved against the page so that the page can change the values for widgets that are displayed - so different data on different pages in different position. 

Am currently on a bit of an performance drive to see if I can eek out some speed.  This has mainly been done by implementing a better cache policy, and so far the tests have been optimistic - using LoadStorm.com has been extremely helpful.

One thing that I thought might have been slow was the JSON serialisation and deserialisation.  Although it wasn't really affect the page traces that I was doing, and most of the page traces were trouncing old sites running the previous version of my CMS.  But I stumbled up JSON.net whilst looking up some stuff on c# 4.0 and with the performance gains over the standard System.Web.Scripting.Serialisation that I was using I thought I would give it a try.

Now I have created 2 extenion methods on the string class :

 

/* PREVIOUS CODE */

public static T FromJson<T>(this string s)
{
     JavaScriptSerializer ser = new JavaScriptSerializer();
     return ser.Deserialize<T>(s);
}

public static string ToJson(this object obj)
{
      JavaScriptSerializer ser = new JavaScriptSerializer();            
      return ser.Serialize(obj);
}

/* NEW CODE WITH JSON.NET */

public static T FromJson<T>(this string s)
{            
     return JsonConvert.DeserializeObject<T>(s);
}

public static string ToJson(this object obj)
{                       
     return JsonConvert.SerializeObject(obj);
}

 

And I don't see any performance gain on my page trace output, even though I am doing a lot of FromJson calls on every page request.  And in many a case with the JSON.net is implemented, the page trace figures are either the same or worse than the page trace figures of the standard version.

Now with the performance gains touted on the JNK blog, I thought I'd ask if I was doing something wrong, or is there something I can do to get this performance increase?  Or am I just looking too deeply into this and the performance gains won't be noticeable until am in the 100s to 1000s range of object deserilisation?

Thanks

 

Coordinator
Mar 17, 2010 at 7:02 AM

JSON serialization with both is really fast. In my test even with 5000 iterations the difference is still in the small milliseconds.

Mar 17, 2010 at 7:29 AM

Hi James,

Yeah I thought that might be the case, so it would be in the large number of iterations to see some real speed benefits from the code library.  At most I do about 10 per request but with data caching that can reduce down to less than 5.  I was only really worried about when the site get's heavily hit, but I don't think we will ever have the traffic to have to worry about JSON being the bottle kneck...will most likely be server and bandwidth.

Thanks for your time, and you have a great library there, it is more "readable" than the standard JSON stuff.

Colin