react-doctor/js-min-max-loop
Use `Math.min(...array)` / `Math.max(...array)` instead of sorting just to read the first or last element
- 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 arr.sort()[0] (literal index 0) or arr.sort()[expr - 1] (BinaryExpression with operator '-' and literal 1 on the right) — i.e. grabbing the smallest or largest after sorting. False positive when .sort() uses a custom comparator that orders by something other than numeric values (string length, date, custom field) — Math.min/max won't replicate that.
Fix prompt
Use this once validation confirms the diagnostic is real.
Replace with Math.min(...arr) or Math.max(...arr) — O(n) instead of O(n log n), and avoids the in-place mutation that .sort() performs on the original array (a common subtle bug). For arrays larger than ~100k elements use a manual for loop to dodge the spread argument-length limit. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min
Related rules
More Performance rules from the rules reference:
react-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 renderreact-doctor/jsx-no-jsx-as-prop: Hoist the inline JSX out of render or memoize it with useMemo so the prop value is stable across renders.react-doctor/jsx-no-new-array-as-prop: Memoize the array (useMemo) or hoist it outside the component instead of allocating a new one each render.