optimize iteration patterns

When implementing loops and iteration logic, evaluate whether your current approach is the most efficient and readable option available. Look for opportunities to simplify complex iteration patterns and eliminate unnecessary loops.

copy reviewer prompt

Prompt

Reviewer Prompt

When implementing loops and iteration logic, evaluate whether your current approach is the most efficient and readable option available. Look for opportunities to simplify complex iteration patterns and eliminate unnecessary loops.

Common optimizations to consider:

  • Use for (let [index, item] of array.entries()) when you need both index and value, rather than manual index tracking
  • Replace loops that handle simple conditional logic with direct conditional statements
  • Consider whether character-by-character or element-by-element processing can be simplified

Example of optimization:

// Instead of manual index tracking:
for (let index = 0; index < lines.length; index++) {
  const line = lines[index];
  // process line and index
}

// Use entries() for cleaner code:
for (let [index, line] of lines.entries()) {
  // process line and index
}

// Instead of unnecessary loops for simple cases:
for (let dx = 1; dx < charWidth; dx++) {
  currentLine[offsetX + dx] = '';
}

// Use direct conditional when appropriate:
if (char.fullWidth) {
  currentLine[offsetX + 1] = '';
}

This approach reduces computational complexity, improves code readability, and often eliminates potential off-by-one errors in index management.

Source discussions