js-index-maps — Claude Code rule: what it does, how to install
The js-index-maps rule replaces repeated .find() calls on one key with a Map index. Use it when array lookups happen many times.
MIT ★ 7,191 updated 2026-10-24
.claude/rules/js-index-maps.mdWhat js-index-maps does
The rule recommends creating a Map once when code repeatedly searches for records by the same key. For instance, users can be indexed by id, then retrieved for each order with get() instead of scanning the users array with .find() again.
Building the index costs O(n), while later Map reads cost O(1). With 1000 orders and 1000 users, the example changes 1M operations to 2K. Keeping .find() inside the order loop leaves every lookup at O(n).
What it helps with
- replace repeated
.find()calls by one key - index users by
id - cut lookup work while processing orders
- spot linear searches inside loops
Example prompts for the agent
- Review the order-processing code and replace repeated
.find()calls byuserIdwith aMap. - Index
usersbyid, then rewrite the user lookup to useget(). - Look for repeated searches by the same key and suggest a
Map-based refactor.
How to install js-index-maps
Manually: put the files in place
.claude/rules/js-index-maps.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-index-maps.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/js-index-maps.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/js-index-maps.mdManually: put the files in place
.clinerules/js-index-maps.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about js-index-maps
How do I install js-index-maps in Claude Code?
Put the rule in .claude/rules/js-index-maps.md.
What does js-index-maps do?
It tells the agent to replace repeated .find() calls for one key with a single Map index and get() lookups.
Why use a Map instead of repeated .find() here?
The index is built in O(n), while each later lookup is O(1). In the example, 1000 orders and 1000 users change 1M operations to 2K.
Source text
js-index-maps.md · MIT
---
title: Build Index Maps for Repeated Lookups
impact: LOW-MEDIUM
impactDescription: 1M ops to 2K ops
tags: javascript, map, indexing, optimization, performance
---
## Build Index Maps for Repeated Lookups
Multiple `.find()` calls by the same key should use a Map.
**Incorrect (O(n) per lookup):**
```typescript
function processOrders(orders: Order[], users: User[]) {
return orders.map(order => ({
...order,
user: users.find(u => u.id === order.userId)
}))
}
```
**Correct (O(1) per lookup):**
```typescript
function processOrders(orders: Order[], users: User[]) {
const userById = new Map(users.map(u => [u.id, u]))
return orders.map(order => ({
...order,
user: userById.get(order.userId)
}))
}
```
Build map once (O(n)), then all lookups are O(1).
For 1000 orders × 1000 users: 1M ops → 2K ops.
© tech-leads-club · MIT · tech-leads-club/agent-skills