Ruby detect vs any?
Calculating -------------------------------------
detect 51.783k i/100ms
any? 61.233k i/100ms
-------------------------------------------------
detect 784.367k (± 7.3%) i/s - 3.936M
any? 1.005M (± 9.9%) i/s - 4.960M
Comparison:
any?: 1004760.7 i/s
detect: 784366.6 i/s - 1.28x slower
require 'benchmark/ips'
string = "foo".freeze
array = [/zoo/, /boo/, /moo/]
Benchmark.ips do |x|
x.report("detect") {
array.detect {|regex| string =~ regex }
}
x.report("any?") {
array.any? {|regex| string =~ regex }
}
x.compare!
end