No commit activity in last 3 years
No release in over 3 years
A monkey patch to speed up Rails' JSON generation time.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16.0
>= 0
~> 12.3.0

Runtime

>= 1.9.3, ~> 1.0
 Project Readme

Rails::Patch::Json::Encode

This is a monkey patch for Rails in order to speed up its JSON encoding, and to draw people's attention to this Rails issue.

For full details please read Jason Hutchens' blog post.

All credits goes to Jason Hutchens for discovering the issue and providing the code for this monkey patch.

Installation

First, let's measure the time before the patch.
Go to your Rails console and type:

require 'benchmark'
DATA = Hash.new
key = 'aaa'
1000.times { DATA[key.succ!] = DATA.keys }
Benchmark.realtime { 5.times { DATA.to_json } }

Then bundle install this gem with a fast JSON encoding gem in your Rails' Gemfile.

gem 'rails-patch-json-encode'
gem 'yajl-ruby', require: 'yajl'

In this case I choose the yajl-ruby gem, but you can choose a json-encoder gem that multi_json supports.

The final step is to choose a patch. Two types of patches are available, and you have to choose one and invoke it explictly:

  • Rails::Patch::Json::Encode.patch_base_classes patches all Ruby base classes.
  • Rails::Patch::Json::Encode.patch_renderers patches Rails' ActionController::Renderers only. This is for those who had issue with the JSON gem, as patching base classes cause infinite recursive loop.

Place one of them in a Rails initializer (e.g. config/initializers/rails_patch_json_encode.rb), and Rails should now use the faster encoder.

Now it's done. Reopen your Rails console and rerun the benchmark to see the difference.

Warning

If you are using Oj gem, there is no need to install this gem. Call Oj.optimize_rails instead.

Rails in recent years added safety nets to handle nested NaN, infinity and IO objects. This gem does not handle these cases.

This gem may break your app. Test your app.

Benchmark

rake benchmark is provided to show the difference before and after the patch. From my machine the time is dropped to 14% when using yajl on Rails 5.2.

The actual performance boost on real-world applications will probably be less than that. For one of my page I see the rendering time dropped by 25%.

What's with the name

This is just a temporal monkey patch, and a monkey patch isn't supposed to have a fancy name.

Related reading

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request