fast-ruby icon indicating copy to clipboard operation
fast-ruby copied to clipboard

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

Open kewlar opened this issue 6 years ago • 6 comments

kewlar avatar Mar 14 '19 08:03 kewlar

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.

kewlar avatar Mar 14 '19 09:03 kewlar

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

KEYS.map{ |key| HASH[key] }

Arcovion avatar Mar 14 '19 09:03 Arcovion

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

kewlar avatar Mar 14 '19 09:03 kewlar

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

  • when all keys are expected to exist in the Hash (Hash#values_at);
  • when some keys may not exist in the Hash, and we care about the non-existing keys (Hash#values_at);
  • when some keys may not exist in the Hash, and we care only about the existing keys (Hash#slice#values).

Thoughts?

kewlar avatar Mar 21 '19 18:03 kewlar

@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.

etagwerker avatar Feb 17 '23 16:02 etagwerker

@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?

kewlar avatar Mar 02 '23 12:03 kewlar