Inefficient Membership Test (ARCH6075)
Key ruby.performance.inefficient-membership-test · Severity warning · Category performance
Checking membership with include? on an array is O(n); repeated in a loop it becomes O(n*m). Use a Set to make the lookup O(1).
Incorrect
Section titled “Incorrect”def allowed(ids, blocked) ids.select { |id| !blocked.include?(id) }endCorrect
Section titled “Correct”require "set"
def allowed(ids, blocked) blocked_set = blocked.to_set ids.select { |id| !blocked_set.include?(id) }endConvert the array to a Set for repeated membership checks and benefit from O(1) lookups.