@@ -593,31 +593,29 @@ Comparison:
593
593
594
594
##### ` Hash#dig ` vs ` Hash#[] ` vs ` Hash#fetch ` [ code] ( code/hash/dig-vs-[]-fetch.rb )
595
595
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
+
596
600
```
597
601
$ ruby -v code/hash/dig-vs-\[\]-vs-fetch.rb
598
602
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
+
606
604
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.096M (± 5.4 %) i/s - 15.539M
611
- Hash#fetch 4.425M (± 5.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.782M (± 4.8 %) i/s - 13.905M in 5.010328s
609
+ Hash#fetch 4.101M (± 6.1 %) i/s - 20.531M in 5.024945s
610
+ Hash#fetch fallback 2.975M (± 5.5 %) i/s - 14.972M in 5.048880s
613
611
614
612
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
621
619
```
622
620
623
621
##### ` Hash[] ` vs ` Hash#dup ` [ code] ( code/hash/bracket-vs-dup.rb )
0 commit comments