Repeated Work In Loop (ARCH6072)
هذا المحتوى غير متوفر بلغتك بعد.
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.
Incorrect
Section titled “Incorrect”def filter(items, config) items.select do |item| item.score > config.thresholds.fetch(:min) endendCorrect
Section titled “Correct”def filter(items, config) min = config.thresholds.fetch(:min) items.select { |item| item.score > min }endCompute loop-invariant expressions once and assign them to a variable before the loop.