typestar

LRU cache with Map in JavaScript

A fixed-size cache that evicts the least recently used entry, built on Map's insertion order.

class LRUCache {
  constructor(capacity) {
    this.capacity = capacity;
    this.map = new Map();
  }

  get(key) {
    if (!this.map.has(key)) return undefined;
    const value = this.map.get(key);
    this.map.delete(key);
    this.map.set(key, value);
    return value;
  }
}

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
243
Tokens
81
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 49 seconds.

Type this snippet

Step 4 of 4 in Closures, step 4 of 16 in Functions & patterns.

← Previous Next →