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

Hash#has_key? vs Set#include?

Open marcotc opened this issue 4 years ago • 0 comments

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.

marcotc avatar Jun 10 '21 21:06 marcotc