-
Notifications
You must be signed in to change notification settings - Fork 377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add benchmark for Hash#dig vs #[] vs #fetch #102
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'benchmark/ips' | ||
|
||
h = { a: { b: { c: { d: { e: "foo" } } } } } | ||
|
||
Benchmark.ips do |x| | ||
x.report 'Hash#dig' do | ||
h.dig(:a, :b, :c, :d, :e) | ||
end | ||
|
||
x.report 'Hash#[]' do | ||
h[:a][:b][:c][:d][:e] | ||
end | ||
|
||
x.report 'Hash#[] fallback' do | ||
((((h[:a] || {})[:b] || {})[:c] || {})[:d] || {})[:e] | ||
end | ||
|
||
x.report 'Hash#fetch' do | ||
h.fetch(:a).fetch(:b).fetch(:c).fetch(:d).fetch(:e) | ||
end | ||
|
||
x.report 'Hash#fetch fallback' do | ||
h.fetch(:a, {}).fetch(:b, {}).fetch(:c, {}).fetch(:d, {}).fetch(:e, nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
h.fetch(:a) { {} }.fetch(:b) { {} }... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your notation will create Procs for every #fetch. That maybe faster or not. Seems that's another test… :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ixti I'd like to keep it as is because I see the current approach more often. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mblumtritt in fact it will not. Why would it create Proc? In any case, @dideler I tend to agree that probably some people use o = {}
h.fetch(:a, o).fetch(:b, o) That's just my thoughts. I don't insist on anything :D |
||
end | ||
|
||
x.compare! | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ouch, do people ever really do it this way? far more common I think is:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure which way is more common. Should I use one example over the other or include both?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I think, the way proposed by @nateberkopec is more effective in case when none of the keys are exists, because you don't need to create all of these hashes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the more common style to the benchmark, see 95b8ea4