DEV Community

Augusts Bautra
Augusts Bautra

Posted on

Use `gem "oj"` for free JSON performance

You're probably generating JSON strings, or parsing them in your Rails app. Using gem "oj" can speed these operations up significantly.

Setting up is easy!

Just include the gem in gemfile, and call Oj.optimize_rails in an initializer.

On my M2 Mac I see a nice 30-40% execution time reduction.

Benchmark.bmbm do |x|
  x.report("jsonification") { 500_000.times { {"yay#{_1}" => true}.to_json } }
end

# without OJ

jsonification   0.929295   0.001351   0.930646 (  0.934957)
jsonification   0.937358   0.001534   0.938892 (  0.944757)
jsonification   0.999621   0.002159   1.001780 (  1.010062)

# with OJ

jsonification   0.605699   0.001421   0.607120 (  0.614704)
jsonification   0.593242   0.001314   0.594556 (  0.601887)
jsonification   0.602174   0.001644   0.603818 (  0.611623)
jsonification   0.587775   0.002133   0.589908 (  0.597829)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)