| 1 | --- |
| 2 | title: Use Set/Map for O(1) Lookups |
| 3 | impact: LOW-MEDIUM |
| 4 | impactDescription: O(n) to O(1) |
| 5 | tags: javascript, set, map, data-structures, performance |
| 6 | --- |
| 7 | |
| 8 | ## Use Set/Map for O(1) Lookups |
| 9 | |
| 10 | Convert arrays to Set/Map for repeated membership checks. |
| 11 | |
| 12 | **Incorrect (O(n) per check):** |
| 13 | |
| 14 | ```typescript |
| 15 | const allowedIds = ['a', 'b', 'c', ...] |
| 16 | items.filter(item => allowedIds.includes(item.id)) |
| 17 | ``` |
| 18 | |
| 19 | **Correct (O(1) per check):** |
| 20 | |
| 21 | ```typescript |
| 22 | const allowedIds = new Set(['a', 'b', 'c', ...]) |
| 23 | items.filter(item => allowedIds.has(item.id)) |
| 24 | ``` |
| 25 |