js-length-check-first — Claude Code rule: faster array comparisons
js-length-check-first — a rule for Claude Code that prompts the AI to check array lengths before running expensive operations like sorting or deep equality checks. If lengths differ, arrays cannot be equal—no expensive work needed.
MIT ★ 7,191 updated 2026-10-24
.claude/rules/js-length-check-first.mdWhat js-length-check-first does
The rule catches comparisons in hot paths: event handlers, render loops, functions called hundreds of times per second. Instead of running O(n log n) sorts on two arrays, a simple O(1) length check returns the result immediately if the arrays differ in size.
The rule includes side-by-side examples in TypeScript. The wrong way sorts and joins arrays regardless of length, wasting time and memory. The right way checks length first, sorts only when lengths match, then compares elements in a loop and exits on the first mismatch.
Install the rule in one step to .claude/rules/ and the code generator starts avoiding pointless computation when working with arrays.
What it helps with
- skip sorting and join() when arrays have different lengths
- speed up array comparisons inside event handlers
- reduce memory use with large arrays
- return a result in O(1) instead of O(n log n)
Example prompts for the agent
- Compare two string arrays and tell me if they changed, without wasting cycles
- Write a function that checks if two lists are identical by looking at size first
- Optimize array comparison so sorting doesn't happen when lengths don't match
How to install js-length-check-first
Manually: put the files in place
.claude/rules/js-length-check-first.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-length-check-first.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/js-length-check-first.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/js-length-check-first.mdManually: put the files in place
.clinerules/js-length-check-first.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about js-length-check-first
How do I install js-length-check-first in Claude Code?
Place the rule file in your project's .claude/rules/ folder as js-length-check-first.md. Claude Code picks it up automatically on the next run.
What does this rule do?
It teaches the code generator to check array lengths before costly operations like sorting or deep comparison. If the lengths differ, the operation is unnecessary and the answer comes back immediately.
When is this rule most useful?
Use it in hot paths—event handlers, render loops, functions that run hundreds of times per second. On large arrays, the savings add up fast.
Source text
js-length-check-first.md · MIT
---
title: Early Length Check for Array Comparisons
impact: MEDIUM-HIGH
impactDescription: avoids expensive operations when lengths differ
tags: javascript, arrays, performance, optimization, comparison
---
## Early Length Check for Array Comparisons
When comparing arrays with expensive operations (sorting, deep equality, serialization), check lengths first. If lengths differ, the arrays cannot be equal.
In real-world applications, this optimization is especially valuable when the comparison runs in hot paths (event handlers, render loops).
**Incorrect (always runs expensive comparison):**
```typescript
function hasChanges(current: string[], original: string[]) {
// Always sorts and joins, even when lengths differ
return current.sort().join() !== original.sort().join()
}
```
Two O(n log n) sorts run even when `current.length` is 5 and `original.length` is 100. There is also overhead of joining the arrays and comparing the strings.
**Correct (O(1) length check first):**
```typescript
function hasChanges(current: string[], original: string[]) {
// Early return if lengths differ
if (current.length !== original.length) {
return true
}
// Only sort when lengths match
const currentSorted = current.toSorted()
const originalSorted = original.toSorted()
for (let i = 0; i < currentSorted.length; i++) {
if (currentSorted[i] !== originalSorted[i]) {
return true
}
}
return false
}
```
This new approach is more efficient because:
- It avoids the overhead of sorting and joining the arrays when lengths differ
- It avoids consuming memory for the joined strings (especially important for large arrays)
- It avoids mutating the original arrays
- It returns early when a difference is found
© tech-leads-club · MIT · tech-leads-club/agent-skills