Back to JavaScript Performance

Build Index Maps for Repeated Lookups

javascriptmapindexingoptimizationperformance
28.0k🕒 2026-06-10Source ↗

Install this skill

npx skills add vercel-labs/agent-skills

Works 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

  1. Identify the source array that requires frequent searching
  2. Initialize a new Map constructor using the entries of the source array
  3. Map the key-value pairs using the unique identifier as the key
  4. Replace the .find() call with a .get() request on the created Map
  5. 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

Why is Map faster than find?
The .find() method scans an array element-by-element, while a Map uses a hash table to jump directly to the memory address associated with a key.
Is this approach always better?
No, if you only need to retrieve a single value, the cost of constructing the Map object will be slower than a single linear scan.
Can I use objects as keys in this map?
Yes, unlike standard object keys that cast to strings, a JavaScript Map maintains the identity of objects used as keys.

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.

Source & trust

28k stars🕒 Updated 2026-06-10
📄 Full skill instructions — original source: vercel-labs/agent-skills
## Build Index Maps for Repeated Lookups

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)

  1. Click "Download" above
  2. In your project, create the directory: .agent/skills/js-index-maps/
  3. Save the file as SKILL.md
  4. 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

Read the Master Guide: Mastering Agent Skills

Recommended Rules

View more rules

Recommended Workflows

View more workflows

Recommended MCP Servers

View more MCP servers

Take It Further

Maximize your productivity with these powerful resources

📋

Define Your Standards

Set up coding standards to ensure this workflow produces consistent, high-quality results.

Browse Rules Library
📖

Master Workflows

Learn how to create custom workflows, use Turbo Mode, and build your automation library.

Complete Guide

How to use this Skill in Claude Code & Cursor

For Claude Code (CLI)

To use this skill in Claude Code, copy the rule content into your project's custom instructions or follow our Add-Skill CLI guide. This ensures Claude follows your standards during every code generation.

For Cursor & Windsurf

For Cursor or Windsurf, individual skills are best used in the "Rules for AI" section. This specific unit helps the agent avoid javascript performance issues, leading to cleaner, more efficient code.

Why the skill format matters: the standardized Agent Skills format lets your AI agent load detailed instructions only when they are relevant, keeping your prompt clean while improving results.

Source & attribution

This skill is categorized under JavaScript Performance and is published by Vercel Engineering, maintained in vercel-labs/agent-skills.

← Browse All Agent Skills
Sponsored AI assistant. Recommendations may be paid.