I recently got into a discussion with a fellow Android developer about passing data between activities. He mentioned that he passes his models around by serializing them with gson. After having this discussion, I got curious and decided to explore and benchmark a few different serialization standards to see how they stack up.

The methodology for this experiment is largely based on a blog post by Philippe Breault. I used the same models and methodology so that I could expand upon his initial comparison.

Serializable
Incredibly easy to integrate into your application and requires no maintenance but it is memory intensive and very slow.

 

Gson (Out of the box configuration + JsonSerializer + TypeAdapter)
Also very easy to integrate but is equally if not slower than serializable. You can optionally provide serializers and deserializers give gson an inexpensive way to work with your models. Using custom serializers and deserializers requires a bit of maintenance.

 

Parcelable
Extremely fast and efficient but requires maintenance.

Parceler An optional library that generates all of your parcelable models essentially giving you the benefits of parcelable without the overhead of maintaining it.

 

The Methodology (Same as Philippe Breault)
Loop over the serialization process 1000 times.
Do this 10 times to get a nice average.
Run on multiple devices using both dalvik and ART

Parcelable is clearly the fastest here. What actually surprised me was that gson is slower than serializable. The slowness is expected because it relies on reflection but I wasn’t expecting it to be slower than serializable.

Just to see what happened, I graphed the same tests but this time comparing dalvik to ART.

The one thing that we can take from this is that parcelable is clearly the fastest way to serialize your data. If you are looking to easily supercharge your app with the least amount of overhead, use parceler. We at Prolific love parcelables and encourage everyone to use them.

Share Button

Comments