Back to Home

How we increased the speed of JSON generation by 6,000 times / Staply Blog

ruby on rails 4 · json · performance optimization

How we increased the speed of JSON generation by 6,000 times

  • Tutorial
A brief overview of how to form JSON

Rails currently has the following methods for serializing objects in JSON:
  1. Call to_json () directly.
  2. RABL
  3. Active model serializers
  4. Jbuilder


The first method is ideal for situations where you do not need to serialize nested objects.
@posts.to_json(:include => [...], :methods => [...])

However, forming a JSON response with a list of models, it becomes necessary to serialize related models in the right form.

JBuilder
According to personal experience, the most organically fitting into Rails development is, included by default in Rails 4, JBuilder.
json.extract! message, :id, :content, :chat_id
json.user do
    json.cache!(["user_", message.user_id]) do
      json.partial! 'users/user', user: message.user
    end
end

RABL
RABL, compared to other gems, seems ancient due to its syntax and clumsy due to the low performance of the monster.
collection :@messages
attributes :id, :content, :chat_id
child(:user) { attributes :name }
node(:read) { |message| message.read_by?(@user) }


ActiveModel :: Serializer
Active model serializers offers to describe JSON in the form of ruby ​​classes, without invented syntax, which reduces the zoo of using various DSLs in the project.

Resolving Performance Issues

Small comparison

Test data: 1000 typical messages with a sender and additional fields.
JSON:
{
id: 1,
content: "Lorem",
chat_id: 1,
created_at: "2014-06-18T21:47:49.363Z",
kind: 0,
formatted_content: "Lorem",
user: {
    id: 1,
    name: "Ivan Petrov",
    image: "https://lh6.googleusercontent.com/photo.jpg"
},
files: [ ],
links: [ ]
}


to_json: 0.2ms
JBuilder: 129.0ms
JBuilder with OJ: 88.0ms

The numbers are averaged, but the overall picture is clear. It makes no sense to compare different gems with each other, since the difference is not significant, but objectively, JBuilder is the fastest to date.
For convenience, you have to pay speed. All described gems in speed lose to to_json () method several times.
Using JBuilder without configuration will cost you ~ 600.0ms.

There are several ways to speed up JBuilder:

  • Connect gems: oj, oj_mimic_json, multi_json.
    Add to initialization MultiJson.use(:oj)
    What will replace the standard serializer with a faster Oj.
  • Do not use partials that significantly slow down JBuilder. Issues have long been created, in which, despite the closed status, this problem has not actually been resolved
  • Cache templates using the built-in cache method in JBuilder!


In the Staply project (almost open beta), we used JBuilder, only at the initial stage of development, gradually moving to the formation of JSON manually using to_json (). Which is a personal demonstration of the whole concept of Rails, providing fairly quick and easy development at the beginning with optimization at the end.

Read Next