Skip to content

CAT 004

unurgunite edited this page Jul 11, 2026 · 3 revisions

CAT-004 — Array#include? in loop → Set

Severity: warningConfidence: highAuto-fix:

Description

Array#include? in loop → Set — Using Array#includes? inside a loop is O(n) per check. Use a Set for O(1) lookups.

Bad code

large_array.each do |x|
  if other_array.includes?(x)
    ...
  end
end

Performance Impact

Tip

This rule can significantly improve performance in hot code paths.

Benchmark

Note

See bench results at bench/cat-004/

Clone this wiki locally