Skip to main content
rule CX Codex

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

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/js-length-check-first.md
Open on GitHub

What 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.md

The paths frontmatter field limits the files

Step 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

Similar IDE rules

All IDE rules for Code review and refactoring →

From the same set130

The whole set →
_sections Defines eight performance optimization sections, ranks them by impact, and guides the agent in prioritizing code improvements. Install once, reference by ID when analyzing bottlenecks. CX Codex +1 ★ 7,191repo stars client-passive-event-listeners client-passive-event-listeners instructs the agent to add { passive: true } to touch and wheel listeners, preventing scroll stutter in browser viewports. CX Codex +1 ★ 7,191repo stars js-cache-storage js-cache-storage keeps localStorage, sessionStorage and cookie values in memory so reads stop hitting synchronous browser storage. CX Codex +1 ★ 7,191repo stars rerender-use-ref-transient-values The rerender-use-ref-transient-values rule teaches the agent to store frequently changing values in useRef instead of useState to avoid re-rendering the component on every update. CX Codex +1 ★ 7,191repo stars js-tosorted-immutable The js-tosorted-immutable rule replaces mutating .sort() with .toSorted(), keeping React props and state arrays unchanged during sorting. CX Codex +1 ★ 7,191repo stars patterns-children-over-render-props patterns-children-over-render-props — a Claude rule that pushes React components toward composition with children instead of renderX props, so call sites read as plain JSX. CX Codex +1 ★ 7,191repo stars js-cache-function-results js-cache-function-results instructs the agent to cache repeated function calls using a module-level Map. It eliminates redundant computations across renders and utility calls. CX Codex +1 ★ 7,191repo stars client-localstorage-schema The client-localstorage-schema rule guides Claude Code to version localStorage keys and keep only fields the UI needs. Use it when changing client-side settings or cached user data. CX Codex +1 ★ 7,191repo stars

Project sponsors

Companies that keep this analytics open