public
Last active

  • Download Gist
hash_access_benchmark.rb
Ruby
1 2 3 4 5 6 7 8
require 'benchmark/ips'
 
h = { foo: :bar }
 
Benchmark.ips do |r|
r.report('#[]') { h[:foo] }
r.report('#fetch') { h.fetch(:foo) }
end
output.txt
1 2 3 4 5 6
Calculating -------------------------------------
#[] 84463 i/100ms
#fetch 58818 i/100ms
-------------------------------------------------
#[] 3004469.2 (±1.6%) i/s - 15034414 in 5.005338s
#fetch 2196062.9 (±0.8%) i/s - 10998966 in 5.008783s

Looks like rb_hash_fetch_m winds up calling rb_scan_args and rb_block_given_p where rb_hash_aref does not. rb_block_given_p could move into the conditional which evaluates it if one doesn't care about the warning. I don't know what you could do about parsing the varargs though.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.