I’m implementing a tiny LRU cache in JavaScript with Map insertion order, but after a get, the next put seems to evict the key I just touched. What am I misunderstanding about how to refresh recency here?
class LRU {
constructor(limit) { this.limit = limit; this.map = new Map(); }
get(key) {
if (!this.map.has(key)) return -1;
return this.map.get(key);
}
put(key, val) {
this.map.set(key, val);
if (this.map.size > this.limit) {
const newest = [...this.map.keys()].pop();
this.map.delete(newest);
}
}
}
WaffleFries