DEV Community

Augusts Bautra
Augusts Bautra

Posted on

3 1 1

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

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more