react-doctor/js-index-maps
Build an index `Map` once outside the loop instead of `array.find(...)` inside it
- Category: Performance
- Severity: warn
- Source:
oxlint-plugin-react-doctor - Framework: global
- Enabled when: always
Validation prompt
Use this to decide whether a fired diagnostic is real or a false positive.
The rule fires on array.find(...) or array.findIndex(...) inside any loop body (uses createLoopAwareVisitors). False positive when the searched array changes per iteration (so a pre-built index can't be reused), or when the array is tiny (< ~10 items) where linear scan beats Map allocation and hashing overhead.
Fix prompt
Use this once validation confirms the diagnostic is real.
Before the loop, build const byId = new Map(items.map(item => [item.id, item])); then use byId.get(targetId) inside — O(1) lookups turn the total cost from O(n*m) into O(n+m). For multi-key lookups, build several Maps or a Map keyed on a composite string. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
Related rules
More Performance rules from the rules reference:
react-doctor/js-length-check-first: Short-circuit with `a.length === b.length && a.every((x, i) => x === b[i])` — unequal-length arrays exit immediatelyreact-doctor/js-min-max-loop: Use `Math.min(...array)` / `Math.max(...array)` instead of sorting just to read the first or last elementreact-doctor/js-set-map-lookups: Use a `Set` or `Map` for repeated membership tests / keyed lookups — `Array.includes`/`find` is O(n) per callreact-doctor/js-tosorted-immutable: Use `array.toSorted()` (ES2023) instead of `[...array].sort()` for immutable sorting without the spread allocationreact-doctor/jsx-no-constructed-context-values: Memoize the context value with useMemo/useCallback or hoist it outside the render