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

Hash#has_key? vs Set#include? #193

Closed marcotc closed 1 year ago

marcotc commented 3 years ago

When selecting a data structure to store a set of unique elements, in which a common operation will be to check if an element is part of that data structure, the a Set comes to mind as the ideal, minimal choice.

One downside of Set is that it's slower than a Hash when checking for present of an element, even though Set's implementation is backed by a Hash.

This PR documents the difference in performance profile between Hash#has_key? and Set#include? to help users that need to check for an association in performance critical paths.

etagwerker commented 1 year ago

A benchmark should use the same starting point. This benchmark is using two different objects. I don't think this PR is a good fit for this project.