So there are many different ways to tell if an array includes an object. The results will change depending on where in the array the object is and if the object is in the array or not. A I once again used a random array to benchmark a few ways to do this.
n = 10000
start_array = Array.new(6000)
(0...6000).each{|i| start_array[i]=rand(4000000)}
require "benchmark"
Benchmark.bm(1) do |test|
test.report("include?:") do
n.times{
start_array.include?(5)
}
end
test.report("for loop:") do
n.times{
found = false
for x in start_array
if x == 5
found = true
break
end
end
}
end
test.report("any:") do
n.times{
start_array.any?{|x| x==5}
}
end
test.report("find:") do
n.times{
true if start_array.find{|x| x==5}
}
end
test.report("select:") do
n.times{
true if start_array.select{|x| x==5}
}
end
end
The results?
I ran the test again but this time I inserted 5 at index 200. No shock they all cut out when they find the object they are looking for.