js-min-max-loop — Claude Code rule: what it does, how to install
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.
no license ★ 1,064 updated 2026-01-21
.claude/rules/js-min-max-loop.mdWhat js-min-max-loop 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.
What it helps with
- 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 for the agent
- Write a helper to get the latest updated project from an array
- Find the oldest and newest records in this list without sorting
How to install js-min-max-loop
Manually: put the files in place
.claude/rules/js-min-max-loop.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/js-min-max-loop.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/js-min-max-loop.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/js-min-max-loop.mdManually: put the files in place
.clinerules/js-min-max-loop.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about js-min-max-loop
How to install js-min-max-loop in Claude Code?
Save the rule file to .claude/rules/js-min-max-loop.md inside your project root directory.
What does js-min-max-loop do?
It guides the assistant to search for extreme array values using an O(n) loop rather than running an O(n log n) array sort.
Why avoid Math.min(...array) for boundary values?
Spreading a very large array into function arguments can exceed JavaScript call stack limits and trigger runtime errors.