Serialization on macOS and iOS - Speed and Size

I am currently working on a new storage for my Objective-C apps and I wondered if there is significant difference in speed and size of various serialization methods popular on macOS and iOS.

The Contestants

On board with Foundation we already got some great ones:

NSJSONSerialization

NSKeyedArchiver

NSPropertyListSerialization in the two flavors XML and Binary

As the newcomer I chose:

MPMessagePackWriter implementing MessagePack

I know, there are others like BSON, Thrift or Avro, but I want to keep it flexible on my side and not define any schema beforehand. I also have in mind, to use the format cross platform, which should not be a problem with JSON and MessagePack. I left the other ones in the test anyway out of curiosity, but they are not the winners anyway as we'll see later ;)

The Setting

I wrote a little Unit Test to perform my non representative tests. I then implemented performance testing parts and a size comparison one. You can take a look at this gist to see what I did.

I also chose a very small test object and a very large one. As I said, this is really not very representative, but it might give an idea.

Finally, I also added GZIP into the mix, just to see if I was over optimizing for my problem.

The Size Results

For the small test MessagePack is the winner. JSON is also doing very well. GZIP is not playing a big role for small size, it is making things even worse, but that was to be expected. Even though XML was expected to be large, I wondered that KeyedArchiver is too.

For the large sizes GZIP really makes a difference. Of course there are a lot of repetitions for the property names which should make a nice target for compression.

But then again MessagePack is the winner and needs almost half as much space as the looser in this race does. But the distance to JSON again is not that big.

A very strange observation is, that the Binary variant of Plist is even worse than XML.

The Speed Results

Green stands for tests on the small data and blue for large one:

For smaller data JSON seems to be the fastest one, followed by Message pack. For larger ones Plist is faster. Keyed Archiver is the slowest one in the field.

The Final

Overall for my personal purposes JSON and MessagePack seem to be the most appropriate ones. I was very positively surprised of the JSON results. MessagePack as the clear winner in the size comparison is probably the best choice for the projects I'm working on.

I was very disappointed of KeyedArchiver, which I previously expected to be in the top field. If not required for Apple OS specific purposes it really does not make sense to use any of those proprietary formats anymore.