Skip to content

Instantly share code, notes, and snippets.

@wspurgin
Created April 12, 2022 21:19
Show Gist options
  • Select an option

  • Save wspurgin/21a651906155254d11de6326288fd119 to your computer and use it in GitHub Desktop.

Select an option

Save wspurgin/21a651906155254d11de6326288fd119 to your computer and use it in GitHub Desktop.
Simple benchmark comparing Oj and JSON::Ext (core ruby)
#! /usr/env ruby
require "benchmark"
require "oj"
require "json"
require "rbconfig"
puts "Host OS: #{RbConfig::CONFIG['host_os']}"
puts "Ruby Version #{RUBY_VERSION}"
puts "OJ version #{Oj::VERSION}"
puts "JSON version #{JSON::VERSION}\n"
json_string = <<~JSON
{"a":"Alpha","b":true,"c":12345,"d":[true,[false,[-123456789,null],3.9676,["Something else.",false],null]],"e":{"zero":null,"one":1,"two":2,"three":[3],"four":[0,1,2,3,4]},"f":null,"h":{"a":{"b":{"c":{"d":{"e":{"f":{"g":null}}}}}}},"i":[[[[[[[null]]]]]]]}
JSON
# use JSON to craft the object (just for a baseline in compatibility)
ruby_obj = JSON.load(json_string)
puts "⚠️ - JSON.load and Oj.load do not produce equivalent JSON" unless ruby_obj == Oj.load(json_string)
n = 100_000
oj_report = nil
json_report = nil
puts "Number of iterations: #{n}"
Benchmark.bm(14, "OJ-per-parse", "JSON-per-parse") do |test|
oj_report = test.report("Oj.load") { n.times { Oj.load(json_string) } }
json_report = test.report("JSON.load") { n.times { JSON.load(json_string) } }
[oj_report/n, json_report/n]
end
puts "Oj parses/sec #{n/oj_report.total}"
puts "JSON parses/sec #{n/json_report.total}"
Benchmark.bm(14, "OJ-per-gen", "JSON-per-gen") do |test|
oj_report = test.report("Oj.dump") { n.times { Oj.dump(ruby_obj) } }
json_report = test.report("JSON.dump") { n.times { JSON.dump(ruby_obj) } }
[oj_report/n, json_report/n]
end
puts "Oj gens/sec #{n/oj_report.total}"
puts "JSON gens/sec #{n/json_report.total}"
@wilsonsilva

Copy link
Copy Markdown

MacBook Pro 2021 with Apple M1 Max 64 GB
MacOS 26.5.1 (25F80)

Run 1:

Host OS: darwin25
Ruby Version 4.0.5
OJ version 3.17.3
JSON version 2.19.8
Number of iterations: 100000
                     user     system      total        real
Oj.load          0.314455   0.002083   0.316538 (  0.317359)
JSON.load        0.883228   0.003778   0.887006 (  0.888897)
OJ-per-parse     0.000003   0.000000   0.000003 (  0.000003)
JSON-per-parse   0.000009   0.000000   0.000009 (  0.000009)
Oj parses/sec 315917.8360891899
JSON parses/sec 112738.80898212641
                     user     system      total        real
Oj.dump          0.131457   0.000817   0.132274 (  0.132351)
JSON.dump        0.108653   0.000227   0.108880 (  0.109031)
OJ-per-gen       0.000001   0.000000   0.000001 (  0.000001)
JSON-per-gen     0.000001   0.000000   0.000001 (  0.000001)
Oj gens/sec 756006.4714153956
JSON gens/sec 918442.3218221904

Run 2:

Host OS: darwin25
Ruby Version 4.0.5
OJ version 3.17.3
JSON version 2.19.8
Number of iterations: 100000
                     user     system      total        real
Oj.load          0.299505   0.001972   0.301477 (  0.301639)
JSON.load        0.866178   0.003440   0.869618 (  0.870688)
OJ-per-parse     0.000003   0.000000   0.000003 (  0.000003)
JSON-per-parse   0.000009   0.000000   0.000009 (  0.000009)
Oj parses/sec 331700.26237490756
JSON parses/sec 114993.01992369065
                     user     system      total        real
Oj.dump          0.131309   0.000900   0.132209 (  0.132342)
JSON.dump        0.107674   0.000403   0.108077 (  0.108204)
OJ-per-gen       0.000001   0.000000   0.000001 (  0.000001)
JSON-per-gen     0.000001   0.000000   0.000001 (  0.000001)
Oj gens/sec 756378.1588242863
JSON gens/sec 925266.2453621025

Run 3:

Host OS: darwin25
Ruby Version 4.0.5
OJ version 3.17.3
JSON version 2.19.8
Number of iterations: 100000
                     user     system      total        real
Oj.load          0.301231   0.002720   0.303951 (  0.304206)
JSON.load        0.863987   0.003965   0.867952 (  0.868023)
OJ-per-parse     0.000003   0.000000   0.000003 (  0.000003)
JSON-per-parse   0.000009   0.000000   0.000009 (  0.000009)
Oj parses/sec 329000.39809048164
JSON parses/sec 115213.7445388685
                     user     system      total        real
Oj.dump          0.136024   0.000441   0.136465 (  0.136702)
JSON.dump        0.106556   0.000752   0.107308 (  0.107311)
OJ-per-gen       0.000001   0.000000   0.000001 (  0.000001)
JSON-per-gen     0.000001   0.000000   0.000001 (  0.000001)
Oj gens/sec 732788.6271205075
JSON gens/sec 931896.9694710544

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment