İçeriğe geç

Döngüde Tekrarlanan İş (ARCH3072)

Anahtar typescript.performance.repeated-work-in-loop · Önem warning · Kategori performance

Girdileri döngü değişkenine bağlı olmayan bir hesaplama her yinelemede aynı sonucu üretir; döngü dışına alınmalıdır (hoisting).

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;
}
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;
}

Döngü değişkenine bağlı olmayan hesaplamaları döngünün dışına bir değişkene taşıyın.