What performance issue does the following code pattern commonly cause?
function processItems(items) {
for (let i = 0; i < items.length; i++) {
// Process each item
console.log(items[i]);
}
}
In this code pattern, items.length is recalculated on every iteration of the loop, which can cause performance issues with large arrays. The JavaScript engine has to look up the length property of the items array in each iteration. A more optimized approach would be to cache the length before the loop: const len = items.length; for (let i = 0; i < len; i++) { ... }. Modern JavaScript engines may optimize this in simple cases, but explicitly caching the length is still considered a best practice, especially for complex loops or when working with DOM collections which may not benefit from the same optimizations.