Performance Test — JSON serializers

NOTE: For an updat­ed set of bench­marks, see the Bench­marks page.

For those of you who have worked with JSON data in .Net you should be famil­iar with the Dat­a­Con­trac­tJ­son­Se­ri­al­iz­er class and the JavaScript­Se­ri­al­iz­er class already. Both allow you to serialize/deserialize an object to and from a piece of string in JSON for­mat, though there are some notable dif­fer­ences.

Besides these two BCL (base class library) JSON seri­al­iz­ers, there are pop­u­lar third-par­ty offer­ing such as Json.Net and a rel­a­tive­ly new­er ServiceStack.Text which also offers its own seri­al­iza­tion for­mat called JSV (JSON + CSV).

It is claimed that ServiceStack.Text’s JSON seri­al­iz­er is 3x faster than Json.Net and 3.6x faster than the BCL JSON seri­al­iz­ers! So, nat­u­ral­ly, I had to test it out for myself and here’s what I found.

Assumptions/Conditions of tests

  1. code is com­piled in release mode, with opti­miza­tion options turned on
  2. 5 runs of the same test is per­formed, with the top and bot­tom results exclud­ed, the remain­ing three results is then aver­aged
  3. 100,000 instances of type Sim­pleOb­ject (see below) is cre­at­ed, each with a dif­fer­ent ID and Name, and then giv­en to the seri­al­iz­ers to seri­al­ize and dese­ri­al­ize
  4. serialization/deserialization of the objects hap­pen sequen­tial­ly in a loop (no con­cur­ren­cy)
 1: [DataContract]
 2: public class SimpleObject
 3: {
 4:     [DataMember]
 5:     public int Id { get; set; }
 6:
 7:     [DataMember]
 8:     public string Name { get; set; }
 9: }

Results

With these assump­tions in mind, here are the aver­age times (in mil­lisec­onds) I record­ed for seri­al­iza­tion and dese­ri­al­iza­tion using each of five JSON seri­al­iz­ers I test­ed:

image

image

Look­ing at these data, it would seem the ServiceStack.Text.JsonSerializer class offers the best speed in both seri­al­iza­tion and dese­ri­al­iza­tion cas­es, how­ev­er the gains are much more mod­est than those adver­tised in the case of seri­al­iza­tion. How­ev­er, it real­ly comes into its own when it comes to dese­ri­al­iza­tion and the speed gains are quite impres­sive indeed!

Update 2011/09/12:

Turns out I had a typo in my per­for­mance test code and I was using Json.Net for the seri­al­iza­tion test for Sim­ple­J­son, my bad, sor­ry folks… So I fixed the typo and ran the tests again and updat­ed the data and graph above with the cor­rect data. As Pra­bir Shrestha point­ed out, by enabled Reflection.Emit you’re able to get much bet­ter per­for­mance out of Sim­ple­J­son and the new test results reflect this as the Sim­ple­J­son test was run whilst Reflection.Emit was enabled.