What is the fastest way to render json in rails

Currently oj seems to be the fastest renderer – beating yajl (according to the oj author’s comparison).

Oj is used by default in the latest multi_json (and rails uses mutli_json by default), so swapping to oj should be as simple as adding the following to your Gemfile:

  # Gemfile
  gem "oj"

Then each time you call render, it will now use oj.

  render :json => { ... } # uses multi_json which uses oj

Oj also provides additional specific interfaces, if you want even more performance, but sticking to multi_json makes it easier to swap out gems in the future.

Note that if you have any { ... }.to_json calls – these will not be upgraded to use oj unless you call Oj.mimic_JSON in an initializer.

Leave a Comment