DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. Map vs Object vs Set vs Array — When to Use What in JavaScript
XLinkedInReddit
MediumFrontend Engineering

Map vs Object vs Set vs Array — When to Use What in JavaScript

D
DevPrep Team
February 9, 2026·3 min read·0
Table of Contents
  • Quick Decision Guide
  • Object vs Map — The Key Differences
  • When to Use Map
  • When to Use Object
  • Array vs Set — The Key Differences
  • When to Use Set
  • Production Patterns
  • Pattern: Feature Flags with Set
  • Performance Comparison — Real Numbers
  • Best Practices

Written by Rahul · Frontend Engineer at Google · Updated 2025

I see developers defaulting to Objects and Arrays for everything. That's like using a hammer for every job. JavaScript gives us 4 main data structures — each optimized for different use cases. Let me show you when to use which one.

Quick Decision Guide

NeedUse
Ordered list of itemsArray
Key-value pairs (string keys)Object
Key-value pairs (any key type)Map
Unique values onlySet

Object vs Map — The Key Differences

FeatureObjectMap
Key typesString/Symbol onlyAny type (objects, functions, etc.)
Key orderNot guaranteed (mostly insertion)Guaranteed insertion order
SizeObject.keys(obj).lengthmap.size (O(1))
Iterationfor...in, Object.entries()for...of, .forEach()
PerformanceSlower for frequent add/deleteOptimized for frequent add/delete
JSON support✅ Native❌ Need manual conversion
PrototypeHas inherited propertiesClean — no inherited keys

When to Use Map

// 1. When keys aren't strings
const componentCache = new Map();
const buttonRef = document.querySelector("button");
componentCache.set(buttonRef, { clicks: 0, renders: 5 });

// 2. When you need to know the size instantly
const userSessions = new Map();
console.log(userSessions.size); // O(1) — Object needs Object.keys().length

// 3. When you add/delete keys frequently
const cache = new Map();
cache.set("user:123", userData);
cache.delete("user:123");
// Map is optimized for this pattern

// 4. LRU Cache implementation
class LRUCache {
  constructor(capacity) {
    this.capacity = capacity;
    this.cache = new Map(); // Map preserves insertion order!
  }
  get(key) {
    if (!this.cache.has(key)) return -1;
    const value = this.cache.get(key);
    this.cache.delete(key);
    this.cache.set(key, value); // Move to end (most recent)
    return value;
  }
  put(key, value) {
    this.cache.delete(key);
    this.cache.set(key, value);
    if (this.cache.size > this.capacity) {
      // Delete the FIRST (oldest) entry
      this.cache.delete(this.cache.keys().next().value);
    }
  }
}

When to Use Object

// 1. API responses — they're already objects
const user = await fetch("/api/user").then(r => r.json());

// 2. Configuration / options
const config = {
  apiUrl: "https://api.example.com",
  timeout: 5000,
  retries: 3,
};

// 3. When you need JSON serialization
JSON.stringify(config); // Works naturally
// JSON.stringify(map);  // Just gives "{}"

Array vs Set — The Key Differences

FeatureArraySet
Duplicates✅ Allowed❌ Automatically unique
Lookup (.has())O(n) — .includes()O(1) — .has()
OrderIndexedInsertion order
Access by index✅ arr[0]❌ No direct index access

When to Use Set

// 1. Remove duplicates — the classic
const ids = [1, 2, 3, 2, 1, 4];
const uniqueIds = [...new Set(ids)]; // [1, 2, 3, 4]

// 2. Fast membership checking
const premiumUsers = new Set(["user1", "user2", "user3"]);
if (premiumUsers.has(currentUser.id)) {
  // O(1) lookup instead of Array.includes() which is O(n)
  showPremiumContent();
}

// 3. Set operations
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([3, 4, 5, 6]);

// Union
const union = new Set([...setA, ...setB]); // {1,2,3,4,5,6}

// Intersection
const intersection = new Set([...setA].filter(x => setB.has(x))); // {3,4}

// Difference
const difference = new Set([...setA].filter(x => !setB.has(x))); // {1,2}

// 4. Tracking visited/seen items
const visited = new Set();
function crawlPage(url) {
  if (visited.has(url)) return; // Already crawled
  visited.add(url);
  // ... crawl
}

Production Patterns

Pattern: Feature Flags with Set

const enabledFeatures = new Set(["dark-mode", "new-editor", "ai-chat"]);

function FeatureGate({ feature, children }) {
  if (!enabledFeatures.has(feature)) return null;
  return children;
}

// Usage

Performance Comparison — Real Numbers

// Lookup performance with 100,000 items:
// Array.includes()  — ~2.5ms
// Set.has()         — ~0.01ms
// Object["key"]     — ~0.01ms
// Map.get("key")    — ~0.01ms

// If you're doing lookups in a loop, Set/Map is 250x faster than Array

Best Practices

  1. Default to Array for ordered collections you'll iterate over
  2. Use Set when uniqueness matters or you need fast lookups
  3. Use Map when you need non-string keys or frequent add/delete
  4. Use Object for structured data with known keys (configs, API responses)
  5. Convert between them freely — [...set], new Set(arr), Object.fromEntries(map)

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • Quick Decision Guide
  • Object vs Map — The Key Differences
  • When to Use Map
  • When to Use Object
  • Array vs Set — The Key Differences
  • When to Use Set
  • Production Patterns
  • Pattern: Feature Flags with Set
  • Performance Comparison — Real Numbers
  • Best Practices

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.