Introduction
It doesn't matter if you're loading a configuration file, data set or even something obtained from a website to use as a data source, there is a performance tradeoff you'll encounter because of the processing time that occurs while parsing JSON or XML. This blog post covers how well loading and reading JSON & XML files using native APIs perform on iOS & Android.
Test Files
To test performance the SouthWind data set was used for JSON and XML. Both data sets are of roughly the same size, 1.3mb, and contain the same information. Source code used for the performance is provided below. The source for each project is quite generic and simply loads the data set file and reads all the employees into an array. All of the timing information is output through log statements.
- Android Project (1.9mb)
- iOS Project (563kb)
Android
For XML loading and reading the DocumentBuilderFactory, DocumentBuilder, Document and NodeList classes were used. For JSON loading and reading the JSONObject and JSONArray classes were used.
Testing was done using a Kindle Fire HDX 7" tablet. The results on Android greatly varied on each run, but JSON was the faster choice by 300 milliseconds and even up to more than a second of processing time.
iOS
For XML loading and reading the NSXMLParser class was used. Because of how NSXMLParser is used it required created a separate class and adopting the NSXMLParserDelegate. For JSON loading and reading the NSJSONSerialization class was used for creating an NSDictionary from the JSON.
Testing was done using an iPad 3. The results on iOS were consistent throughout the run and provided JSON quite the edge over XML by more than a second for each of the runs.
Conclusion
As you might of guessed JSON APIs are a lot faster at loading and reading compared to XML APIs. Picking which option to use requires you to look at the entire solution you're working toward. For example, a lot of companies make use of XLST transformations with their XML which can be very powerful. If you're app doesn't need a format with a lot of overhead for extra search functionality & transformation, then JSON is the way to go. The open source community has really done a great job extending what's possible with JSON.