<!--
title: avoid redundant computations
domain: app-frameworks
topic: Performance Optimization
language: JavaScript
source: mastodon/mastodon
updated: 2024-09-23
url: https://awesomereviewers.com/reviewers/mastodon-avoid-redundant-computations/
-->

Identify and eliminate computations that are performed repeatedly when they could be executed once. This includes moving loop-invariant checks outside of loops and reducing callback overhead that creates unnecessary function declarations.

Common patterns to optimize:
- **Loop invariants**: Move conditions that don't depend on individual loop items outside the loop
- **Repeated function calls**: Cache results of expensive operations that don't change
- **Callback overhead**: Minimize complex function declarations that cause GC churn

Example from file upload validation:
```javascript
// Before: checking upload limit for every file
filesArray.some(file => {
  // ... file-specific checks ...
  || (files.length + media.size + pending > maxMediaAttachments)  // This doesn't depend on individual file!
})

// After: check upload limit once before processing files
if (files.length + media.size + pending > maxMediaAttachments) {
  // handle error
  return;
}
// Then process individual files without redundant check
```

This optimization reduces computational overhead and improves performance, especially in scenarios with loops, callbacks, or frequently called functions.
