Skip to content

Commit d6e1ac0

Browse files
didelerJuanitoFatas
authored andcommitted
📝 Introduce Hash#dig and link to more info
Closes #102
1 parent 0681495 commit d6e1ac0

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

README.md

+17-19
Original file line numberDiff line numberDiff line change
@@ -593,31 +593,29 @@ Comparison:
593593

594594
##### `Hash#dig` vs `Hash#[]` vs `Hash#fetch` [code](code/hash/dig-vs-[]-fetch.rb)
595595

596+
[Ruby 2.3 introduced `Hash#dig`](http://ruby-doc.org/core-2.3.0/Hash.html#method-i-dig) which is a readable
597+
and performant option for retrieval from a nested hash, returning `nil` if an extraction step fails.
598+
See [#102 (comment)](https://github.com/JuanitoFatas/fast-ruby/pull/102#issuecomment-198827506) for more info.
599+
596600
```
597601
$ ruby -v code/hash/dig-vs-\[\]-vs-fetch.rb
598602
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
599-
Warming up --------------------------------------
600-
Hash#dig 142.217k i/100ms
601-
Hash#[] 153.313k i/100ms
602-
Hash#[] || 145.380k i/100ms
603-
Hash#[] && 121.401k i/100ms
604-
Hash#fetch 137.236k i/100ms
605-
Hash#fetch fallback 120.010k i/100ms
603+
606604
Calculating -------------------------------------
607-
Hash#dig 6.216M (± 6.2%) i/s - 31.003M
608-
Hash#[] 6.676M (± 6.3%) i/s - 33.269M
609-
Hash#[] || 6.160M (± 6.2%) i/s - 30.675M
610-
Hash#[] && 3.096M5.4%) i/s - 15.539M
611-
Hash#fetch 4.425M5.5%) i/s - 22.095M
612-
Hash#fetch fallback 3.279M (± 5.3%) i/s - 16.441M
605+
Hash#dig 5.719M (± 6.1%) i/s - 28.573M in 5.013997s
606+
Hash#[] 6.066M (± 6.9%) i/s - 30.324M in 5.025614s
607+
Hash#[] || 5.366M (± 6.5%) i/s - 26.933M in 5.041403s
608+
Hash#[] && 2.782M4.8%) i/s - 13.905M in 5.010328s
609+
Hash#fetch 4.101M6.1%) i/s - 20.531M in 5.024945s
610+
Hash#fetch fallback 2.975M (± 5.5%) i/s - 14.972M in 5.048880s
613611
614612
Comparison:
615-
Hash#[]: 6676415.9 i/s
616-
Hash#dig: 6215966.7 i/s - same-ish: difference falls within error
617-
Hash#[] ||: 6160177.6 i/s - same-ish: difference falls within error
618-
Hash#fetch: 4424551.0 i/s - 1.51x slower
619-
Hash#fetch fallback: 3278599.3 i/s - 2.04x slower
620-
Hash#[] &&: 3096090.4 i/s - 2.16x slower
613+
Hash#[]: 6065791.0 i/s
614+
Hash#dig: 5719290.9 i/s - same-ish: difference falls within error
615+
Hash#[] ||: 5366226.5 i/s - same-ish: difference falls within error
616+
Hash#fetch: 4101102.1 i/s - 1.48x slower
617+
Hash#fetch fallback: 2974906.9 i/s - 2.04x slower
618+
Hash#[] &&: 2781646.6 i/s - 2.18x slower
621619
```
622620

623621
##### `Hash[]` vs `Hash#dup` [code](code/hash/bracket-vs-dup.rb)

code/hash/dig-vs-[]-vs-fetch.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
require 'benchmark/ips'
1+
require "benchmark/ips"
22

33
h = { a: { b: { c: { d: { e: "foo" } } } } }
44

55
Benchmark.ips do |x|
6-
x.report 'Hash#dig' do
6+
x.report "Hash#dig" do
77
h.dig(:a, :b, :c, :d, :e)
88
end
99

10-
x.report 'Hash#[]' do
10+
x.report "Hash#[]" do
1111
h[:a][:b][:c][:d][:e]
1212
end
1313

14-
x.report 'Hash#[] ||' do
14+
x.report "Hash#[] ||" do
1515
((((h[:a] || {})[:b] || {})[:c] || {})[:d] || {})[:e]
1616
end
1717

18-
x.report 'Hash#[] &&' do
18+
x.report "Hash#[] &&" do
1919
h[:a] && h[:a][:b] && h[:a][:b][:c] && h[:a][:b][:c][:d] && h[:a][:b][:c][:d][:e]
2020
end
2121

22-
x.report 'Hash#fetch' do
22+
x.report "Hash#fetch" do
2323
h.fetch(:a).fetch(:b).fetch(:c).fetch(:d).fetch(:e)
2424
end
2525

26-
x.report 'Hash#fetch fallback' do
26+
x.report "Hash#fetch fallback" do
2727
h.fetch(:a, {}).fetch(:b, {}).fetch(:c, {}).fetch(:d, {}).fetch(:e, nil)
2828
end
2929

0 commit comments

Comments
 (0)