Aller au contenu

Repeated Work In Loop (ARCH6072)

Ce contenu n’est pas encore disponible dans votre langue.

Key ruby.performance.repeated-work-in-loop · Severity warning · Category performance

A computation whose inputs do not depend on the loop variable is wasted work when rerun each iteration. Hoist the invariant expression out of the loop.

def filter(items, config)
items.select do |item|
item.score > config.thresholds.fetch(:min)
end
end
def filter(items, config)
min = config.thresholds.fetch(:min)
items.select { |item| item.score > min }
end

Compute loop-invariant expressions once and assign them to a variable before the loop.