10LOC

#caching

Node.jsadvanced

A zero-dependency LRU cache built on Map's insertion order

export const createLRUCache = <K, V>(capacity: number) => {
  const cache = new Map<K, V>();

  const get = (key: K) => {
    if (!cache.has(key)) return undefined;

A Map already tracks insertion order — re-inserting on access is enough to get real LRU eviction for free, no library needed.

Reactadvanced

Suspense data fetching with a thrown promise cache

type Resource<T> = { read: () => T };

const cache = new Map<string, Resource<unknown>>();

export const getResource = <T,>(key: string, fetchData: () => Promise<T>): Resource<T> => {

Build the tiny promise cache that makes Suspense-based data fetching work: throw while pending, cache the result, return it once resolved.