Build Index Maps for Repeated Lookups
Install this skill
npx skills add vercel-labs/agent-skillsWorks across Claude Code, Cursor, Codex, Copilot & Antigravity
This skill optimizes data processing by transforming linear search operations into constant-time lookups using JavaScript Map objects. When code iterates through collections to match related items, repeated use of array methods like find() triggers nested loops that degrade performance as dataset sizes increase. This pattern replaces that inefficiency by pre-processing the target collection into a keyed Map once. By establishing an index, the application accesses specific records instantly rather than scanning entire arrays multiple times. This approach reduces execution time from quadratic to linear complexity, significantly speeding up data-heavy tasks such as mapping user IDs to profiles, correlating relational database records in memory, or merging disjoint object arrays. It ensures that functions handling large numbers of entities remain responsive by minimizing unnecessary CPU cycles during repetitive lookups.
When to Use This Skill
- •Mapping user profiles to a large set of order records
- •Joining fragmented database entities before rendering UI lists
- •Cross-referencing configuration objects based on unique keys
- •Synchronizing state across disparate data sources during transformation
How to Invoke This Skill
Example prompts that trigger this skill in Claude Code, Cursor, or Antigravity:
- “speed up this nested find call
- “optimize my array lookup performance
- “how to improve performance of map on a large array
- “convert array to map for faster searching
- “refactor this code to avoid O(n^2) complexity
What this skill does
- •Transforms array-based linear searches into O(1) hash map lookups
- •Reduces total operation count by pre-indexing source collections
- •Handles complex object matching without nested iteration
- •Improves execution speed for functions processing large data batches
When not to use it
- ✕When the array length is consistently small, such as under 50 elements
- ✕When items are only accessed exactly once during the process
Example workflow
- Identify the source array that requires frequent searching
- Initialize a new Map constructor using the entries of the source array
- Map the key-value pairs using the unique identifier as the key
- Replace the .find() call with a .get() request on the created Map
- Measure the execution speed to verify the performance gain
Prerequisites
- –Data structure with a consistent unique identifier
- –Intermediate knowledge of JavaScript Map syntax
Pitfalls & limitations
- !Overhead of creating the Map can exceed benefits if the search is only performed once
- !Maps consume more memory than simple arrays, which may affect heap usage in memory-constrained environments
FAQ
How it compares
While manual loops are readable for beginners, this approach shifts performance logic to the data structure level, preventing the common trap of accidental quadratic complexity found in generic array chaining.
📄 Full skill instructions — original source: vercel-labs/agent-skills
Multiple
.find() calls by the same key should use a Map.**Incorrect (O(n) per lookup):**
function processOrders(orders: Order[], users: User[]) {
return orders.map(order => ({
...order,
user: users.find(u => u.id === order.userId)
}))
}**Correct (O(1) per lookup):**
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.
How to Use This Skill Unit
Option A: Project-Specific (Recommended)
- Click "Download" above
- In your project, create the directory:
.agent/skills/js-index-maps/ - Save the file as
SKILL.md - The agent will automatically discover the skill based on its description.
Option B: Global Installation (All Agents)
Save the file to these locations to make it available across all projects:
- Claude Code:
~/.claude/skills/vercel-labs/agent-skills/js-index-maps/SKILL.md - Cursor:
~/.cursor/skills/vercel-labs/agent-skills/js-index-maps/SKILL.md - Antigravity:
~/.gemini/antigravity/skills/vercel-labs/agent-skills/js-index-maps/SKILL.md
🚀 Install with CLI:npx skills add vercel-labs/agent-skills