Skip to content

LruMap

Defined in: packages/data/src/cache/lru-map.ts:36

A fixed-capacity map that evicts the least recently used entry to make room.

The Map-shaped counterpart of LruCache, over the same engine. Prefer this one: a cache almost always has something to say about each key, and a LruMap<K, undefined> is the degenerate case LruCache covers. Reach for LruCache only when the membership itself is the whole answer — a seen-recently set, a dedupe window.

get counts as a use; has and peek do not, so an inspection cannot reshuffle the eviction order behind the caller’s back. set never throws and never blocks: it answers with whatever entry it had to displace. Overwriting a key already held never evicts.

Time complexity: O(1) for get, set, has, peek and delete. Iteration is most-recently-used first, and does not count as a use.

Keys are compared by SameValueZero, native Map semantics.

K

The key type.

V extends DefinedValue

The value type.

new LruMap<K, V>(iterable, options): LruMap<K, V>

Defined in: packages/data/src/cache/lru-map.ts:47

Iterable<readonly [K, V], any, any> | undefined

LruMapOptions

LruMap<K, V>

readonly capacity: number

Defined in: packages/data/src/cache/lru-map.ts:39

The maximum number of elements the collection can hold.

Note: Infinity is not a legal value here — an unbounded type omits this mixin.

IReplacingMap.capacity


readonly overflowPolicy: ReplacementPolicy = "lru"

Defined in: packages/data/src/cache/lru-map.ts:45

Always "lru". A different replacement policy is a different class, the same way BoundedQueue/LossyQueue are, never a constructor flag.

IReplacingMap.overflowPolicy

[iterator](): Iterator<readonly [K, V]>

Defined in: packages/data/src/cache/lru-map.ts:62

Most recently used first. Does not count as a use.

Iterator<readonly [K, V]>

IReplacingMap.[iterator]


clear(): void

Defined in: packages/data/src/cache/lru-map.ts:70

Removes all elements from the collection.

void

IReplacingMap.clear


count(): number

Defined in: packages/data/src/cache/lru-map.ts:66

The number of elements in the collection.

A method, not a getter: its async counterpart needs a round trip to the backing store, and a member must have the same shape in both.

number

IReplacingMap.count


delete(key): boolean

Defined in: packages/data/src/cache/lru-map.ts:102

O(1) — no tombstone, no compaction.

K

boolean

IReplacingMap.delete


entries(): IterableIterator<readonly [K, V]>

Defined in: packages/data/src/cache/lru-map.ts:117

Most recently used first.

IterableIterator<readonly [K, V]>

IReplacingMap.entries


get(key): V | undefined

Defined in: packages/data/src/cache/lru-map.ts:75

O(1). Counts as a use.

K

V | undefined

IReplacingMap.get


has(key): boolean

Defined in: packages/data/src/cache/lru-map.ts:97

O(1). Does not count as a use.

K

boolean

IReplacingMap.has


keys(): IterableIterator<K>

Defined in: packages/data/src/cache/lru-map.ts:107

Most recently used first.

IterableIterator<K>

IReplacingMap.keys


lru(): readonly [K, V] | undefined

Defined in: packages/data/src/cache/lru-map.ts:127

The least recently used entry — the one set would evict next. O(1). Does not count as a use.

readonly [K, V] | undefined

The entry, or undefined when the map is empty.


peek(key): V | undefined

Defined in: packages/data/src/cache/lru-map.ts:80

O(1). Does not count as a use, unlike get.

K

V | undefined


set(key, value): readonly readonly [K, V][]

Defined in: packages/data/src/cache/lru-map.ts:90

O(1). Counts as a use.

K

V

readonly readonly [K, V][]

The entry evicted to make room, in a one-element array, or an empty array if nothing was evicted. Overwriting a held key never evicts.

IReplacingMap.set


values(): IterableIterator<V>

Defined in: packages/data/src/cache/lru-map.ts:112

Most recently used first.

IterableIterator<V>

IReplacingMap.values