Back to all reviewers

Avoid repeated object creation

TanStack/router
Based on 2 comments
TypeScript

Prevent performance degradation by avoiding the creation of functions, objects, regular expressions, and other expensive operations inside loops or frequently executed code paths. Instead, hoist these creations outside the loop or to a higher scope where they can be reused.

Performance Optimization TypeScript

Reviewer Prompt

Prevent performance degradation by avoiding the creation of functions, objects, regular expressions, and other expensive operations inside loops or frequently executed code paths. Instead, hoist these creations outside the loop or to a higher scope where they can be reused.

This optimization is particularly important in performance-critical sections like routing logic, data processing loops, and render functions where the same operation may be executed hundreds or thousands of times.

Examples of what to avoid:

// Bad: Function created on every iteration
for (const [index, match] of matches.entries()) {
  function makeMaybe(value, error) {
    if (error) return { status: 'error', error }
    return { status: 'success', value }
  }
  // use makeMaybe...
}

// Bad: Regex created on every filter call
items.filter(d => !d.name.match(new RegExp(pattern, 'g')))

Better approach:

// Good: Function hoisted outside the loop
function makeMaybe(value, error) {
  if (error) return { status: 'error', error }
  return { status: 'success', value }
}

for (const [index, match] of matches.entries()) {
  // use makeMaybe...
}

// Good: Regex created once
const regex = new RegExp(pattern, 'g')
items.filter(d => !d.name.match(regex))

This practice reduces memory allocation overhead, garbage collection pressure, and improves execution speed in performance-sensitive code paths.

2
Comments Analyzed
TypeScript
Primary Language
Performance Optimization
Category

Source Discussions