# js-min-max-loop

js-min-max-loop instructs the agent to pick minimum or maximum values with a single loop instead of sorting. Avoids O(n log n) sorting costs and unnecessary array copies.

- rule · `type=rule` · Claude Code, Codex, Cursor, GitHub Copilot, Windsurf (Devin Desktop), Cline · ★ 1064 repo stars
- Author: jarrodwatts
- License: not stated · policy=link-only · description only; the file stays with the author
- Source: https://github.com/jarrodwatts/claude-code-config/tree/HEAD/skills/vercel-react-best-practices/rules/js-min-max-loop.md
- Updated: 2026-01-21
- Security check: automated, 2026-11-16 (text) — no signs of malicious code found.
- HTML: https://affpapa.org/en/nevibecode/rules/jarrodwatts-js-min-max-loop

## What it does

This guideline prevents the assistant from calling `.sort()` solely to extract extreme values. Running a full sort for an earliest date or highest ID incurs O(n log n) cost where a simple pass suffices. The rule guides the agent to write a standard `for` loop with O(n) performance.

When retrieving both boundary elements at once, the code tracks min and max in one pass without cloning the array. Methods like `Math.min(...items)` are discouraged on large lists because spread operations fail when call stack limits are reached.

## Install

### Claude Code (native platform)

**Manually: put the files in place**
- File: `.claude/rules/js-min-max-loop.md` — The paths frontmatter field limits the files

### Codex

**AGENTS.md**
- Codex reads AGENTS.md: paste the rule text there

### Cursor

**Manually: put the files in place**
- File: `.cursor/rules/js-min-max-loop.mdc` — Frontmatter: description, globs, alwaysApply

### GitHub Copilot

**Manually: put the files in place**
- File: `.github/instructions/js-min-max-loop.instructions.md` — The applyTo field sets which files it applies to

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/rules/js-min-max-loop.md`

### Cline

**Manually: put the files in place**
- File: `.clinerules/js-min-max-loop.md`

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/jarrodwatts/claude-code-config/tree/HEAD/skills/vercel-react-best-practices/rules/js-min-max-loop.md

## When to use it
- Replace full `.sort()` calls when looking for extreme elements
- Find both minimum and maximum values in a single loop
- Prevent call stack overflow errors caused by `Math.min(...spread)`

## Example prompts
- Write a helper to get the latest updated project from an array
- Find the oldest and newest records in this list without sorting

## Similar
- [js-performance-patterns](https://affpapa.org/en/nevibecode/skills/js-performance-patterns.md) — skill · 716 installs
- [frontend-js-best-practices](https://affpapa.org/en/nevibecode/skills/frontend-js-best-practices.md) — skill · 341 installs
- [computer-science-algorithms](https://affpapa.org/en/nevibecode/skills/computer-science-algorithms.md) — skill · 205 installs
- [js-request-idle-callback](https://affpapa.org/en/nevibecode/rules/js-request-idle-callback.md) — rule · ★ 1216 repo stars
- [client-passive-event-listeners](https://affpapa.org/en/nevibecode/rules/client-passive-event-listeners.md) — rule · ★ 7191 repo stars

From the same set: [jarrodwatts/claude-code-config](https://affpapa.org/en/nevibecode/by/jarrodwatts/claude-code-config.md) — 20 cards

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
