Skip to content

Instantly share code, notes, and snippets.

@wspurgin
Created April 12, 2022 21:19
Show Gist options
  • Save wspurgin/21a651906155254d11de6326288fd119 to your computer and use it in GitHub Desktop.
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}"
@mathieujobin
Copy link

$ ruby ~/bin/benchmark_json_parsing.rb 
Host OS: linux
Ruby Version 2.7.6
OJ version 3.13.23
JSON version 2.6.3
Number of iterations: 100000
                     user     system      total        real
Oj.load          0.571476   0.000000   0.571476 (  0.571610)
JSON.load        1.064273   0.000000   1.064273 (  1.064650)
OJ-per-parse     0.000006   0.000000   0.000006 (  0.000006)
JSON-per-parse   0.000011   0.000000   0.000011 (  0.000011)
Oj parses/sec 174985.47620547493
JSON parses/sec 93960.85402899444
                     user     system      total        real
Oj.dump          0.220334   0.000000   0.220334 (  0.220351)
JSON.dump        0.464333   0.000000   0.464333 (  0.464392)
OJ-per-gen       0.000002   0.000000   0.000002 (  0.000002)
JSON-per-gen     0.000005   0.000000   0.000005 (  0.000005)
Oj gens/sec 453856.41798360663
JSON gens/sec 215362.68152382024

...

Host OS: linux
Ruby Version 3.2.1
OJ version 3.14.2
JSON version 2.6.3
Number of iterations: 100000
                     user     system      total        real
Oj.load          0.684006   0.000000   0.684006 (  0.684854)
JSON.load        0.847102   0.000000   0.847102 (  0.847177)
OJ-per-parse     0.000007   0.000000   0.000007 (  0.000007)
JSON-per-parse   0.000008   0.000000   0.000008 (  0.000008)
Oj parses/sec 146197.54797472537
JSON parses/sec 118049.53830825565
                     user     system      total        real
Oj.dump          0.193254   0.000000   0.193254 (  0.193261)
JSON.dump        0.438868   0.000000   0.438868 (  0.438933)
OJ-per-gen       0.000002   0.000000   0.000002 (  0.000002)
JSON-per-gen     0.000004   0.000000   0.000004 (  0.000004)
Oj gens/sec 517453.7137653036
JSON gens/sec 227858.9461979457

@stevehill1981
Copy link

❯ ruby json_benchmark.rb
Host OS: darwin24
Ruby Version 3.3.6
OJ version 3.16.7
JSON version 2.9.0
Number of iterations: 100000
                     user     system      total        real
Oj.load          0.252782   0.009167   0.261949 (  0.261958)
JSON.load        0.218810   0.005573   0.224383 (  0.224385)
OJ-per-parse     0.000003   0.000000   0.000003 (  0.000003)
JSON-per-parse   0.000002   0.000000   0.000002 (  0.000002)
Oj parses/sec 381753.7001477387
JSON parses/sec 445666.5611922472
                     user     system      total        real
Oj.dump          0.117130   0.004890   0.122020 (  0.123199)
JSON.dump        0.133768   0.005153   0.138921 (  0.138924)
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 819537.7806916893
JSON gens/sec 719833.5744775808

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