Skip to content

Instantly share code, notes, and snippets.

@tadejm
Created March 9, 2018 13:24
Show Gist options
  • Save tadejm/f970bae728520c93b753b9470354b011 to your computer and use it in GitHub Desktop.
Save tadejm/f970bae728520c93b753b9470354b011 to your computer and use it in GitHub Desktop.
Benchmark JSON serialization
require 'benchmark/ips'
require "json"
require "oj"
require "hanami/utils/json"
require "multi_json"
MultiJson.adapter = :oj
puts "MultiJson.current_adapter => #{MultiJson.current_adapter}"
Benchmark.ips do |x|
x.time = 5
x.warmup = 2
hash = { foo: :bar }
x.report("json") { JSON.dump(hash) }
x.report("multi_json") { MultiJson.dump(hash) }
x.report("oj") { Oj.dump(hash) }
x.report("hanami_json") { Hanami::Utils::Json.generate(hash) }
x.compare!
end
$ ruby benchmark.rb
MultiJson.current_adapter => MultiJson::Adapters::Oj
Warming up --------------------------------------
json 25.510k i/100ms
multi_json 30.283k i/100ms
oj 195.267k i/100ms
hanami_json 30.262k i/100ms
Calculating -------------------------------------
json 276.929k (± 5.8%) i/s - 1.403M in 5.086634s
multi_json 333.933k (± 7.4%) i/s - 1.696M in 5.111007s
oj 3.808M (± 6.6%) i/s - 18.941M in 5.008266s
hanami_json 326.919k (± 5.7%) i/s - 1.634M in 5.018832s
Comparison:
oj: 3808256.4 i/s
multi_json: 333932.8 i/s - 11.40x slower
hanami_json: 326918.8 i/s - 11.65x slower
json: 276929.4 i/s - 13.75x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment