ZippyJSON
A much faster version of JSONDecoder.
Benchmarks
These benchmarks were done on a Macbook Pro. The results are very similar on the iPhone (ZippyJSON is 3x+ faster for all 3 files on both platforms).
Usage
Because it is drop-in, simply add the library to your project and replace JSONDecoder
with ZippyJSONDecoder
anywhere you want to use it. The library is feature-complete, so it will support any use cases you have.
Why is it so much faster?
- Apple's version first converts the JSON into an
NSDictionary
usingNSJSONSerialization
and then afterwards makes things Swifty. The creation of that intermediate dictionary is expensive. - ZippyJSON is built largely in C++ (but still with a Swift interface wrapped around it). For the initial parsing (you might call it tokenizing), it uses simdjson, a very fast library that makes good use of vectorization. Apple, on the other hand, uses entirely Swift (aside from the use of
NSJSONSerialization
) which is generally slower. - There are many specific optimizations in there as well. For example, date parsing for ISO-8601 dates is 10x faster due to using JJLISO8601DateFormatter instead of Apple's date formatter.
So, it's largely due to Apple trying to be elegant and operate at a higher level.
When should you use this library?
At first, default to using JSONDecoder
. It's very battle-tested, and for plenty of use cases is just fine. Then, once you start looking for new things to optimize, take a look at how long your JSON parsing is taking. After all, JSON parsing can be a bottleneck for getting data to the user. As a rule of thumb, divide its current time taken by 4 to approximate the time taken with ZippyJSON. If that difference is significant to you (and even milliseconds can impact a user experience!), then consider using ZippyJSON.
Stability
ZippyJSON uses all the same unit tests that Apple uses for their JSONDecoder, and then some, so it should generally work. However, it is a new release. Feel free to submit a ticket if you find any issues.
Future improvements
There are still many places in the code that are ripe for optimization. Feel free to submit a ticket if you have a specific case where you need more performant JSON parsing, and where ZippyJSON is not already 4x faster than Apple's. JSONEncoder and NSJSONSerialization are also promising for optimzation, please chime in if you need one of these improved.
Installation
Cocoapods
ZippyJSON is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod 'ZippyJSON'
Carthage
To install it, add the following to your Cartfile:
github "michaeleisel/ZippyJSON"