Skip to content

Instantly share code, notes, and snippets.

@pocheptsov
Created October 30, 2021 18:59
Show Gist options
  • Save pocheptsov/5fc02e4dcfac74e4c62fbcdb4a855767 to your computer and use it in GitHub Desktop.
Save pocheptsov/5fc02e4dcfac74e4c62fbcdb4a855767 to your computer and use it in GitHub Desktop.
Ruby Hash merge benchmark
require 'benchmark/ips'
h = { a: 1 }
=> {:a=>1}
Benchmark.ips do |x|
x.report('merge') { {b: 12}.merge(h) }
x.report('merge!') { {b: 12}.merge!(h) }
x.report('merge ** ') { {b: 12, **h} }
x.compare!
end
Warming up --------------------------------------
merge 293.453k i/100ms
merge! 424.041k i/100ms
merge ** 382.969k i/100ms
Calculating -------------------------------------
merge 3.169M (±14.5%) i/s - 15.553M in 5.016664s
merge! 4.351M (± 9.9%) i/s - 21.626M in 5.021534s
merge ** 3.748M (± 9.1%) i/s - 18.765M in 5.051223s
Comparison:
merge!: 4350698.3 i/s
merge ** : 3748037.4 i/s - same-ish: difference falls within error
merge: 3168824.0 i/s - 1.37x (± 0.00) slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment