Skip to content

Instantly share code, notes, and snippets.

@fractaledmind
Created October 28, 2024 17:34
Show Gist options
  • Save fractaledmind/b309ba91fa9f0fe52e87a7399f162f56 to your computer and use it in GitHub Desktop.
Save fractaledmind/b309ba91fa9f0fe52e87a7399f162f56 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
require 'oj'
require 'json'
puts "Ruby version: #{RUBY_VERSION}"
puts "Oj version: #{Oj::VERSION}"
puts "JSON version: #{JSON::VERSION}"
# Sample data to hash - using varied data types
test_data = [1, "string", { a: 1, b: 2 }, [3, 4, 5]]
Benchmark.ips do |x|
x.config(time: 5, warmup: 2)
x.report("Oj") do
Oj.dump(test_data)
end
x.report("JSON") do
JSON.dump(test_data)
end
x.compare!
end
$ ruby bench.rb
Ruby version: 3.3.4
Oj version: 3.16.6
JSON version: 2.7.4
ruby 3.3.4 (2024-07-09 revision be1089c8ec) [arm64-darwin21]
Warming up --------------------------------------
Oj 403.745k i/100ms
JSON 126.032k i/100ms
Calculating -------------------------------------
Oj 4.114M (± 1.9%) i/s (243.07 ns/i) - 20.591M in 5.006862s
JSON 1.259M (± 3.0%) i/s (794.42 ns/i) - 6.302M in 5.010755s
Comparison:
Oj: 4114033.6 i/s
JSON: 1258780.8 i/s - 3.27x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment