UNPKG

ox

Version:

Ethereum Standard Library

53 lines 1.38 kB
/** * Map with a LRU (Least recently used) policy. * * @see https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU * @internal */ export class LruMap extends Map { maxSize; constructor(size) { super(); this.maxSize = size; } get(key) { const value = super.get(key); if (super.has(key) && value !== undefined) { this.delete(key); super.set(key, value); } return value; } set(key, value) { super.set(key, value); if (this.maxSize && this.size > this.maxSize) { const firstKey = this.keys().next().value; if (firstKey) this.delete(firstKey); } return this; } } /** * Map with a bounded FIFO eviction policy. Cheaper than {@link LruMap} on hot * `get` paths because reads do not reorder entries; only writes touch the * eviction queue. * * @internal */ export class BoundedMap extends Map { maxSize; constructor(size) { super(); this.maxSize = size; } set(key, value) { if (!super.has(key) && this.maxSize && this.size >= this.maxSize) { const firstKey = this.keys().next().value; if (firstKey !== undefined) this.delete(firstKey); } return super.set(key, value); } } //# sourceMappingURL=lru.js.map