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-index-maps.md
1---2title: Build Index Maps for Repeated Lookups3impact: LOW-MEDIUM4impactDescription: 1M ops to 2K ops5tags: javascript, map, indexing, optimization, performance6---78## Build Index Maps for Repeated Lookups910Multiple `.find()` calls by the same key should use a Map.1112**Incorrect (O(n) per lookup):**1314```typescript15function processOrders(orders: Order[], users: User[]) {16return orders.map(order => ({17...order,18user: users.find(u => u.id === order.userId)19}))20}21```2223**Correct (O(1) per lookup):**2425```typescript26function processOrders(orders: Order[], users: User[]) {27const userById = new Map(users.map(u => [u.id, u]))2829return orders.map(order => ({30...order,31user: userById.get(order.userId)32}))33}34```3536Build map once (O(n)), then all lookups are O(1).37For 1000 orders × 1000 users: 1M ops → 2K ops.38