Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 95b8ea4

Browse files
committedMar 18, 2016
Also benchmark the more common Hash#[] && style
1 parent a759a25 commit 95b8ea4

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed
 

‎README.md

+18-15
Original file line numberDiff line numberDiff line change
@@ -579,24 +579,27 @@ Comparison:
579579
$ ruby -v code/hash/dig-vs-\[\]-vs-fetch.rb
580580
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
581581
Warming up --------------------------------------
582-
Hash#dig 144.192k i/100ms
583-
Hash#[] 148.853k i/100ms
584-
Hash#[] fallback 149.761k i/100ms
585-
Hash#fetch 132.257k i/100ms
586-
Hash#fetch fallback 120.420k i/100ms
582+
Hash#dig 142.217k i/100ms
583+
Hash#[] 153.313k i/100ms
584+
Hash#[] || 145.380k i/100ms
585+
Hash#[] && 121.401k i/100ms
586+
Hash#fetch 137.236k i/100ms
587+
Hash#fetch fallback 120.010k i/100ms
587588
Calculating -------------------------------------
588-
Hash#dig 6.253M (± 5.9%) i/s - 31.145M
589-
Hash#[] 6.733M (± 5.9%) i/s - 33.641M
590-
Hash#[] fallback 6.209M (± 5.7%) i/s - 31.001M
591-
Hash#fetch 4.500M (± 5.0%) i/s - 22.484M
592-
Hash#fetch fallback 3.330M (± 4.7%) i/s - 16.618M
589+
Hash#dig 6.216M (± 6.2%) i/s - 31.003M
590+
Hash#[] 6.676M (± 6.3%) i/s - 33.269M
591+
Hash#[] || 6.160M (± 6.2%) i/s - 30.675M
592+
Hash#[] && 3.096M (± 5.4%) i/s - 15.539M
593+
Hash#fetch 4.425M (± 5.5%) i/s - 22.095M
594+
Hash#fetch fallback 3.279M (± 5.3%) i/s - 16.441M
593595
594596
Comparison:
595-
Hash#[]: 6732624.6 i/s
596-
Hash#dig: 6252809.1 i/s - same-ish: difference falls within error
597-
Hash#[] fallback: 6209365.5 i/s - same-ish: difference falls within error
598-
Hash#fetch: 4499831.0 i/s - 1.50x slower
599-
Hash#fetch fallback: 3330397.7 i/s - 2.02x slower
597+
Hash#[]: 6676415.9 i/s
598+
Hash#dig: 6215966.7 i/s - same-ish: difference falls within error
599+
Hash#[] ||: 6160177.6 i/s - same-ish: difference falls within error
600+
Hash#fetch: 4424551.0 i/s - 1.51x slower
601+
Hash#fetch fallback: 3278599.3 i/s - 2.04x slower
602+
Hash#[] &&: 3096090.4 i/s - 2.16x slower
600603
```
601604

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

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
h[:a][:b][:c][:d][:e]
1212
end
1313

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

18+
x.report 'Hash#[] &&' do
19+
h[:a] && h[:a][:b] && h[:a][:b][:c] && h[:a][:b][:c][:d] && h[:a][:b][:c][:d][:e]
20+
end
21+
1822
x.report 'Hash#fetch' do
1923
h.fetch(:a).fetch(:b).fetch(:c).fetch(:d).fetch(:e)
2024
end

0 commit comments

Comments
 (0)
Please sign in to comment.