Repeated Work In Loop (ARCH3072)
هذا المحتوى غير متوفر بلغتك بعد.
Key typescript.performance.repeated-work-in-loop · Severity warning · Category performance
A computation whose inputs do not depend on the loop variable yields the same result every iteration and should be hoisted out of the loop.
Incorrect
Section titled “Incorrect”function filter(items: Item[], config: Config): Item[] { const result: Item[] = []; for (const item of items) { if (item.size > config.limits.max * config.factor) { result.push(item); } } return result;}Correct
Section titled “Correct”function filter(items: Item[], config: Config): Item[] { const threshold = config.limits.max * config.factor; const result: Item[] = []; for (const item of items) { if (item.size > threshold) { result.push(item); } } return result;}Move computations that do not depend on the loop variable into a variable outside the loop.