JSON Libraries for iOS Comparison [Updated]

Yesterday I spent some time creating a simple test for JSON libraries and I wrote a quick article about it.

After few hours since the article has been published, I had a lot of visits on my blog, so I decide to improve the quality of the code and invest some time in getting more accurate results. I received a couple of good comments and the first one that made me think a lot was this: > Some of these number don’t look right. Did you compile on release with no assertions?

I have to be honest: I completely missed that point, I forgot to disable assertion or use a ‘Release’ build configuration. I decide to do all the tests again and, thank to some suggestions from steipete in Github, I added the possibility to decide how many times the library will parse the file, this is for more accurate results and some useful stats like average, minimum and maximum. Adding some stats is good, but it’s better if you have some kind of graphic to represent these results, so I decided to add a graphic chart if the phone is turned in landscape after the parsing (thanks to HighCharts).

Just fixing the assertion problem, results are now looking more accurate and they seem to be what I was expecting when I started this project. Let’s see what are the new results starting from the twitter_timeline.json, using an iPhone 4S with iOS 5.0.1 (parsed 50 times):

Twitter Timeline

Now JSONKit is the top class library, the fastest one! NSJSONSerialization is the second one, following with a small gap and NextiveJSON is now the third, really close to the 2 top libraries. Now SBJSON is the 4th library and TouchJSON is the slowest, like in the previous test with the build configuration mistake.

Let’s see the result in a chart: Twitter Timeline Chart

Now it’s the time of repeat.json, the json file with a repeated structure. I did this test parsing the same file 100 times, this is a quite fast parse, so it’s not a big deal for an iPhone 4S.

Repeat

The results are the same of the previous test: JSONKit is the fastest library, followed again by NSJSONSerialization and NextiveJSON. SBJSON and Touch JSON are, again, the 2 slowest libraries.

Again, let’s see the chart: Repeat Chart

Now it’s the time of the hardest json file, the one with 1000 objects with a random structure. I parsed this file only 5 times, because the process is longer than the other 2 and I think that 5 times is the minimum number for having a good stat.

Random

Even here, JSONKit is the best, doing better than NSJSONSerialization and NextiveJSON, both are following with a small gap. Always behind SBJSON and TouchJSON.

And… guess what? Chart time! Random Chart

Conclusion

After using a release build configuration, things changed a lot, now the results are more accurate and using a chart to describe the results, it’s more clear the gap between the fastest library and the worst library.

At this point and without doubts, JSONKit is the best library, followed by NSJSONSerialization and NextiveJSON. SBJSON and TouchJSON are a little bit slower than the 3 bests libraries.

Next time I’ll do the serialization test, updating this project that has been update in Github. Like the previous post, feel free to comment what you think and suggest improvements.