Until now information about the page architecture was combined from several JSON responses. To serialize them we were using jsonapi_serializer – a community-maintained fork of Netflix/fast_jsonapi, which for years was considered a benchmark of efficiency among Ruby developers.
Even though with jsonapi_serializer we could provide decent performance, we still saw some room for development, so we decided to rebuild our endpoints.
To improve efficiency we decided to combine all endpoints into one to provide one JSON response – the result was a rather large tree since in our page architecture there are several levels of children objects.
Initially, we sticked with jsonapi_serializer and strictly followed JSON:API standard. The result with flattened relations was not very readable but the response time was considerably shorter. Still, we wanted to check other options since last articles which compared Ruby serializers efficiency (like this one from Paweł Dąbrowski) were written several years ago and we suspected that since then the landscape could be changed a bit.
To check serializers efficiency we set up a simple test – using Benchmark we serialized component-heavy page 10 times in a row in order to get the appropriate average time.
In the first test, we compared jsonapi_serializer against the built-in Rails serializer active_model_serializers. The average time for one serialization was:
Although we encountered some opinions that jsonapi_serializer is not optimized to handle many relations, this result was a total surprise to us.
In the next step, we tested our page with less-known, newer serializers. We chose Panko and Alba (which brand itself as “the fastest JSON serializer for Ruby”). These two results were even more promising, as the average times were:
That means that both serializers were 10 times faster than the jsonapi_serializer for our use case.
Our tests showed that, at least in the case of relationship-heavy structures, over the years jsonapi_serializer was outmatched by several other libraries, including the built-in Rails serializer.
As for our choice - despite Panko being slightly faster, in the end, we decided to use Alba for two reasons. Alba offers concise and powerful DSL which allows users to heavily customize JSON structure and naming. This, coupled with extensive documentation, makes it a great serializer for developers to work with.