Ir al contenido

Regex Recompiled In Loop (ARCH6073)

Esta página aún no está disponible en tu idioma.

Key ruby.performance.regex-recompiled-in-loop · Severity warning · Category performance

Building a pattern with Regexp.new inside a loop body recompiles the regex on every iteration. Compile the constant pattern once outside the loop.

def matches(lines, word)
lines.select do |line|
line =~ Regexp.new("\\b#{word}\\b")
end
end
def matches(lines, word)
pattern = Regexp.new("\\b#{word}\\b")
lines.select { |line| line =~ pattern }
end

Compile the regular expression once outside the loop or hot path and reuse it.