New
Introducing React Doctor for Enterprise

react-doctor/js-combine-iterations

Combine `.map().filter()` (or similar chains) into a single pass with `.reduce()` or a `for...of` loop to avoid iterating the array twice

  • 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 chained calls where both methods are in {map, filter, forEach, flatMap} — e.g. arr.map(fn).filter(pred) or arr.filter(pred).map(fn). The exact arr.map(fn).filter(Boolean) and arr.map(fn).filter(x => x) shapes are intentionally skipped (covered by js-flatmap-filter). False positive when the intermediate array is genuinely used elsewhere, or N is tiny enough that the extra pass is negligible.

Fix prompt

Use this once validation confirms the diagnostic is real.

Collapse into one pass with arr.flatMap(item => keep(item) ? [transform(item)] : []), arr.reduce((acc, item) => ..., []), or a plain for...of loop pushing into one output array. Avoids both the intermediate allocation and the second full traversal. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap

More Performance rules from the rules reference:

  • react-doctor/js-early-exit: Add an early `return` / `continue` to flatten deep nesting and short-circuit when the predicate is already known
  • react-doctor/js-flatmap-filter: Use `.flatMap(item => condition ? [value] : [])` — transforms and filters in a single pass instead of creating an intermediate array
  • react-doctor/js-hoist-intl: Hoist `new Intl.NumberFormat(...)` to module scope or wrap in `useMemo` — Intl constructors allocate dozens of objects per locale lookup
  • react-doctor/js-hoist-regexp: Hoist `new RegExp(...)` (or large regex literals) to a module-level constant so it isn't recompiled on every loop iteration
  • react-doctor/js-index-maps: Build an index `Map` once outside the loop instead of `array.find(...)` inside it