Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ActiveSupport's HashWithIndifferentAccess access benchmark vs common Ruby Hash
#####################
# Ruby Version: 2.1.6
#####################
# gem 'activesupport', '=3.2.22'
# gem 'activesupport', '=4.2.5'
require 'active_support/all'
require 'benchmark/ips'
Benchmark.ips do |x|
x.config(time: 5, warmup: 2)
@hash_with_indifferent_access = ActiveSupport::HashWithIndifferentAccess.new(a: 1)
@hash = Hash[a: 2]
######
# Hash
######
x.report("Common Hash#fetch access") do
@hash.fetch :a
end
x.report("Common Hash#[] access") do
@hash[:a]
end
###########################
# HashWithIndifferentAccess
###########################
x.report("HashWithIndifferentAccess#fetch Symbol") do
@hash_with_indifferent_access.fetch :a
end
x.report("HashWithIndifferentAccess#fetch String") do
@hash_with_indifferent_access.fetch 'a'
end
x.report("HashWithIndifferentAccess #[] Symbol") do
@hash_with_indifferent_access[:a]
end
x.report("HashWithIndifferentAccess #[] String") do
@hash_with_indifferent_access['a']
end
#########################################
# Hash#with_indifferent_access convertion
#########################################
x.report("Hash#with_indifferent_access #fetch Symbol") do
@hash.with_indifferent_access.fetch :a
end
x.report("Hash#with_indifferent_access #fetch String") do
@hash.with_indifferent_access.fetch 'a'
end
x.report("Hash#with_indifferent_access #[] Symbol") do
@hash.with_indifferent_access[:a]
end
x.report("Hash#with_indifferent_access #[] String") do
@hash.with_indifferent_access['a']
end
# Compare the iterations per second of the various reports!
x.compare!
end
▶ ruby hash_with_indifferent_access_benchmark.rb
Calculating -------------------------------------
Common Hash#fetch access
48.573k i/100ms
Common Hash#[] access
46.887k i/100ms
HashWithIndifferentAccess#fetch Symbol
41.125k i/100ms
HashWithIndifferentAccess#fetch String
41.433k i/100ms
HashWithIndifferentAccess #[] Symbol
35.580k i/100ms
HashWithIndifferentAccess #[] String
46.442k i/100ms
Hash#with_indifferent_access #fetch Symbol
22.616k i/100ms
Hash#with_indifferent_access #fetch String
21.547k i/100ms
Hash#with_indifferent_access #[] Symbol
19.568k i/100ms
Hash#with_indifferent_access #[] String
21.981k i/100ms
-------------------------------------------------
Common Hash#fetch access
5.751M (±11.7%) i/s - 28.318M
Common Hash#[] access
7.998M (±12.2%) i/s - 39.244M
HashWithIndifferentAccess#fetch Symbol
2.086M (± 8.4%) i/s - 10.364M
HashWithIndifferentAccess#fetch String
2.131M (± 6.5%) i/s - 10.607M
HashWithIndifferentAccess #[] Symbol
1.478M (± 8.2%) i/s - 7.365M
HashWithIndifferentAccess #[] String
3.846M (± 9.6%) i/s - 19.041M
Hash#with_indifferent_access #fetch Symbol
436.058k (± 7.9%) i/s - 2.171M
Hash#with_indifferent_access #fetch String
435.556k (± 7.1%) i/s - 2.176M
Hash#with_indifferent_access #[] Symbol
407.470k (± 7.2%) i/s - 2.035M
Hash#with_indifferent_access #[] String
503.255k (± 7.2%) i/s - 2.506M
Comparison:
Common Hash#[] access: 7997771.0 i/s
Common Hash#fetch access: 5750854.4 i/s - 1.39x slower
HashWithIndifferentAccess #[] String: 3845945.3 i/s - 2.08x slower
HashWithIndifferentAccess#fetch String: 2130717.4 i/s - 3.75x slower
HashWithIndifferentAccess#fetch Symbol: 2085604.3 i/s - 3.83x slower
HashWithIndifferentAccess #[] Symbol: 1478029.7 i/s - 5.41x slower
Hash#with_indifferent_access #[] String: 503255.0 i/s - 15.89x slower
Hash#with_indifferent_access #fetch Symbol: 436057.9 i/s - 18.34x slower
Hash#with_indifferent_access #fetch String: 435555.6 i/s - 18.36x slower
Hash#with_indifferent_access #[] Symbol: 407469.7 i/s - 19.63x slower
▶ ruby hash_with_indifferent_access_benchmark.rb
Calculating -------------------------------------
Common Hash#fetch access
47.253k i/100ms
Common Hash#[] access
50.406k i/100ms
HashWithIndifferentAccess#fetch Symbol
39.602k i/100ms
HashWithIndifferentAccess#fetch String
41.055k i/100ms
HashWithIndifferentAccess #[] Symbol
35.924k i/100ms
HashWithIndifferentAccess #[] String
44.995k i/100ms
Hash#with_indifferent_access #fetch Symbol
19.965k i/100ms
Hash#with_indifferent_access #fetch String
19.782k i/100ms
Hash#with_indifferent_access #[] Symbol
20.016k i/100ms
Hash#with_indifferent_access #[] String
23.079k i/100ms
-------------------------------------------------
Common Hash#fetch access
6.053M (±10.4%) i/s - 29.864M
Common Hash#[] access
8.306M (±10.1%) i/s - 40.980M
HashWithIndifferentAccess#fetch Symbol
1.932M (±12.8%) i/s - 9.465M
HashWithIndifferentAccess#fetch String
1.994M (± 7.6%) i/s - 9.935M
HashWithIndifferentAccess #[] Symbol
1.422M (± 5.6%) i/s - 7.113M
HashWithIndifferentAccess #[] String
3.804M (± 9.7%) i/s - 18.808M
Hash#with_indifferent_access #fetch Symbol
410.034k (± 5.4%) i/s - 2.056M
Hash#with_indifferent_access #fetch String
414.702k (± 6.3%) i/s - 2.077M
Hash#with_indifferent_access #[] Symbol
381.347k (± 5.6%) i/s - 1.902M
Hash#with_indifferent_access #[] String
470.877k (± 6.0%) i/s - 2.354M
Comparison:
Common Hash#[] access: 8306261.5 i/s
Common Hash#fetch access: 6053147.2 i/s - 1.37x slower
HashWithIndifferentAccess #[] String: 3803546.0 i/s - 2.18x slower
HashWithIndifferentAccess#fetch String: 1993671.6 i/s - 4.17x slower
HashWithIndifferentAccess#fetch Symbol: 1932004.0 i/s - 4.30x slower
HashWithIndifferentAccess #[] Symbol: 1422367.3 i/s - 5.84x slower
Hash#with_indifferent_access #[] String: 470876.8 i/s - 17.64x slower
Hash#with_indifferent_access #fetch String: 414701.6 i/s - 20.03x slower
Hash#with_indifferent_access #fetch Symbol: 410033.7 i/s - 20.26x slower
Hash#with_indifferent_access #[] Symbol: 381347.2 i/s - 21.78x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment