This article was first composed by Peter Raboud
Multiple methods of parsing JSON data are available to iOS developers, but it is not clear which of these libraries is the best option. We investigated this problem by benchmarking the performance of several JSON libraries for iOS:
- YAJL
- Touch JSON
- SBJson (previously json-framework)
- the native JSON parsing library (only available in iOS 5.0+)
The tests were done by parsing several different-sized sample data multiple times with each of the libraries, and measuring wall time and CPU time as performance metrics. The tests were run on a 4th generation iPod Touch running iOS 5.1 (9B176). Since wall time and CPU time are strongly correlated, with a correlation coefficient of 0.999999, we did not present the CPU time data.
From testing, we discovered that the native method performed the best. The next fastest library is YAJL, which took about twice as long as the native library. Both of these methods only required a single line of code to use, and the native method is even easier, since it doesn’t have any dependencies to import.
One potential issue that we discovered is that the YAJL library can cause issues if using ARC. Due to changes in ARC, weak
and assign
are now mutually exclusive properties, which breaks some of the code in YAJLParser.h
. Line 152 reads “@property (assign, nonatomic) _weak id delegate;
“. Simply deleting weak
seems to fix this problem. While this issue has been open on Github for 7 months at the time of writing, this workaround is the only solution yet discovered, and the author has yet to respond to this issue.
To conclude, the native library is the best choice as far as performance goes, but it can only be used in apps that exclusively target iOS 5.0+. For apps that must provide legacy support, YAJL is the best choice.
About the Author