Saturday 9 August 2014

Json.NET vs JavascriptSerializer performance comparison

In this post, I will talk about Json.NET and the native JavascriptSerializer libraries.
Json.NET, mostly known under NewtonSoft.Json is a library specialized into de/serialization to json format. The library becomes a standard as from VS 2012 as jquery was as from VS 2010.It is available here.
JavascrtipSerializer is a .NET native object  doing the same stuff as Json.NET, this class belongs to the  System.Web.Script.Serialization namespace.

As seen in the home page of Json.NET, it seems to be 250% faster than the javascriptSerializer. In this post, I will try to build a litte enterprise level scenario to see what really happens in background.

Version used :
- Json.NET 6.0.0.0, the latest release at writing
- .NET Framework 4.0

In this labs, I will use two simples objects named SuperObject and ObjectChild

SuperObject has :
- 20 string properties 
-  20 double properties
- 20 datetime? properties
- 20 List<ObjectChild> properties

ObjectChild has :
- 20 string properties 
- 20 double properties
- 20 datetime? properties

Its simulates a massive structure and hierachy that is close to industrial scenario : object are fetched from the database, all of its properties are set, all dependencies are loaded and the final object is serialized ( JsonResult, WEB.API ) into Json then send to the client (view).

I have filled all those properties automatically by using some reflexion trick.
- Guid string was injected to all string type
- DateTime.Now for all DateTime? type
- A large number for the double type

All tests are done on a DELL quad core i7 720qm laptop which allow me to use multithreading on the test program. The full source code is at the end of this post.


Results :
 

 For a small object size ( 1 to 10 nested object ), JavascriptSerializer is bit faster than the Json.NET. The difference is around 2/3, so I have to consider that for a simple structure with a small data, JavascriptSerializer is more effficient  and more lightweight ( see diagram below).
When the data increase, my choice goes on Json.NET serializer. It becomes very efficient tool if we want to get data faster. For 1000 nested object number, Json.NET takes "only" 7500 ms to execute, JavascriptSerializer 23seconds.


The both tools serialize object to json format. The json string length is different. JavascriptSerializer wins every time on this metric. If you need to return a lightweight data, use JavascriptSerializer. It is usefull if the client is on a low-bandwidth location.
As example for 1000 nested object number, Json.NET produces 65464018 chars and 61503685 for JSerializer, a difference of 33960333 chars !

 For the same object serialized earlier, the deserialization result is unexpected. In all of my study case, the JavascriptSerializer performs very quickly. So in all deserialization , I will use JavascriptSerializer.Deserialize method.



What happens if I use Json.NET for serialization and JavascriptSerializer for deserialization ?
No issue. Even if the produced json string  is not the same, each of these tools can independently deserialize the string to the right object without data loose.

Conclusion:
- When deserializing, use JavascriptSerializer.Deserialize
- If reducing the amount of data is a must, using JavascriptSerializer may do the job
- For small data, it's up to you.
- For complex and heavy structure of data, use Json.NET to serialize.
- For optimization, think to serialize with Json.NET and deserialize with JavascriptSerializer.


No comments:

Post a Comment