liveBook · Manning

Map

As explained in Acing the Frontend Interview

A map data structure stores key-value associations while exposing operations for lookup and maintaining an insertion order in the JavaScript implementation described.

In JavaScript and TypeScript, Map stores key-value pairs in insertion order. An LRU cache uses that order to track recency: refresh an accessed key by deleting and resetting it, then evict the first key when capacity is exceeded. section 6.4

Listing 6.6 LRU cache implementation in TypeScript
class LRUCache<K, V> {
  private cache: Map<K, V>;
  private capacity: number;
   constructor(capacity: number) {
    if (capacity <= 0) {
      throw new Error('Capacity must be greater than 0');
    }
    this.capacity = capacity; #A
    this.cache = new Map(); #B
  }
  get(key: K): V | -1 { #C
    if (!this.cache.has(key)) { #D
      return -1;
    }
    const value = this.cache.get(key)!; #E
    this.cache.delete(key);
    this.cache.set(key, value);
    return value;
  }
  put(key: K, value: V): void { #F
    if (this.cache.has(key)) {
      this.cache.delete(key); #G
    }
    this.cache.set(key, value); #H
    if (this.cache.size > this.capacity) {
      const lruKey = this.cache.keys().next().value!; #I
      this.cache.delete(lruKey); #J
    }
  }
  entries(): [K, V][] { #K
    return Array.from(this.cache.entries());
  }
}

The implementation shows Map operations for lookup, recency updates, insertion, and eviction in a TypeScript LRU cache. section 6.4

How Acing the Frontend Interview covers it

How deep it goes
Explains lookup, recency updates, insertion, and eviction; raises concurrent access without detailing a solution.
Examples
Demonstrates a TypeScript LRU cache and a React demo where a get operation updates recency.
What you can do after
Build an O(1) LRU cache using JavaScript or TypeScript Map operations for lookup, recency updates, and eviction.
How it connects
Connects Map to LRU caching, hash maps, and cache eviction.

Also explained in

Compare how 49 books explain Map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Acing the Frontend Interview. The section links point to the parts of the book it draws on.