fastruby / fast-ruby

:dash: Writing Fast Ruby :heart_eyes: -- Collect Common Ruby idioms.
https://github.com/fastruby/fast-ruby
5.67k stars 376 forks source link

Add Hash#values_at vs Array#map { Hash#[] } comparison #174

Open kewlar opened 5 years ago

kewlar commented 5 years ago

DO NOT MERGE YET.

Just noticed that #values_at and #slice#values return different results when Hash members are missing 🤦‍♂️

Will update the code and the benchmark, and see if it's still worth merging.

Arcovion commented 5 years ago

This is faster than slice on my machine and returns the same as values_at:

KEYS.map{ |key| HASH[key] }
kewlar commented 5 years ago

@Arcovion, cool! Will add it to the benchmark, too. Thanks!

kewlar commented 5 years ago

Added more ways of slicing Hash values, and organized them into three separate cases:

Thoughts?

etagwerker commented 1 year ago

@kewlar I see the value in adding code/hash/values_at-vs-map.rb because it is quite straightforward and natural. But I don't see the value in adding the other benchmarks. The other examples are doing too many things and it is hard to tell which part makes one slower than the other.

I'm happy to add code/hash/values_at-vs-map.rb if you submit just that in this PR or another PR.

kewlar commented 1 year ago

@etagwerker, thanks for the feedback!

You're right, the other benchmarks are really just edge cases, and don't contribute much. And Hash#values_at is the best performing in them anyway.

I've removed the redundant benchmarks. How does it look now?