Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Don't Convert Arrays to Sets for Inclusion Checks
ids = (1..1_000_000).to_a; nil
x = rand(1_000_000)
Benchmark.realtime do
10.times do
ids.to_set.include?(x)
end
end
# => 14.3837828636169
Benchmark.realtime do
the_set = ids.to_set
10.times do
the_set.include?(x)
end
end
# => 1.35075497627258
Benchmark.realtime do
10.times do
ids.include?(x)
end
end
# => 0.142719984054565
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.