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}"
@wspurgin
Copy link
Author

Example output as of 2022-04-12:

Host OS: darwin19
Ruby Version 3.0.3
OJ version 3.13.11
JSON version 2.6.1
Number of iterations: 100000
                     user     system      total        real
Oj.load          0.460000   0.000802   0.460802 (  0.461237)
JSON.load        0.679385   0.001759   0.681144 (  0.681957)
OJ-per-parse     0.000005   0.000000   0.000005 (  0.000005)
JSON-per-parse   0.000007   0.000000   0.000007 (  0.000007)
Oj parses/sec 217012.9469924176
JSON parses/sec 146811.8342083319
                     user     system      total        real
Oj.dump          0.260016   0.003615   0.263631 (  0.263895)
JSON.dump        0.513998   0.003328   0.517326 (  0.517703)
OJ-per-gen       0.000003   0.000000   0.000003 (  0.000003)
JSON-per-gen     0.000005   0.000000   0.000005 (  0.000005)
Oj gens/sec 379318.0619881576
JSON gens/sec 193301.70917371253

@mjobin-mdsol
Copy link

$ ruby ~/bin/benchmark_json_parsing.rb
Host OS: darwin21
Ruby Version 3.1.3
OJ version 3.14.2
JSON version 2.6.3
Number of iterations: 100000
                     user     system      total        real
Oj.load          0.524510   0.003058   0.527568 (  0.529602)
JSON.load        0.741753   0.004862   0.746615 (  0.749507)
OJ-per-parse     0.000005   0.000000   0.000005 (  0.000005)
JSON-per-parse   0.000007   0.000000   0.000007 (  0.000007)
Oj parses/sec 189549.0249598156
JSON parses/sec 133937.83944871183
                     user     system      total        real
Oj.dump          0.220785   0.007687   0.228472 (  0.230952)
JSON.dump        0.494558   0.008106   0.502664 (  0.505236)
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 437690.39532196504
JSON gens/sec 198940.0474273074

....

$ ruby ~/bin/benchmark_json_parsing.rb
Host OS: darwin21
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.610070   0.002647   0.612717 (  0.615312)
JSON.load        0.750300   0.004231   0.754531 (  0.756516)
OJ-per-parse     0.000006   0.000000   0.000006 (  0.000006)
JSON-per-parse   0.000008   0.000000   0.000008 (  0.000008)
Oj parses/sec 163207.48404238827
JSON parses/sec 132532.65936058294
                     user     system      total        real
Oj.dump          0.208553   0.001904   0.210457 (  0.211426)
JSON.dump        0.452026   0.003591   0.455617 (  0.456434)
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 475156.4452596012
JSON gens/sec 219482.59173823625

@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

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