Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply 62 React and Next.js performance optimization rules from Vercel Engineering
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/js-early-exit.md
1---2title: Early Return from Functions3impact: LOW-MEDIUM4impactDescription: avoids unnecessary computation5tags: javascript, functions, optimization, early-return6---78## Early Return from Functions910Return early when result is determined to skip unnecessary processing.1112**Incorrect (processes all items even after finding answer):**1314```typescript15function validateUsers(users: User[]) {16let hasError = false17let errorMessage = ''1819for (const user of users) {20if (!user.email) {21hasError = true22errorMessage = 'Email required'23}24if (!user.name) {25hasError = true26errorMessage = 'Name required'27}28// Continues checking all users even after error found29}3031return hasError ? { valid: false, error: errorMessage } : { valid: true }32}33```3435**Correct (returns immediately on first error):**3637```typescript38function validateUsers(users: User[]) {39for (const user of users) {40if (!user.email) {41return { valid: false, error: 'Email required' }42}43if (!user.name) {44return { valid: false, error: 'Name required' }45}46}4748return { valid: true }49}50```51