# Why does this LRU cache evict the most recently used key instead of the least recently used one?

**URL:** <https://forum.kirupa.com/t/why-does-this-lru-cache-evict-the-most-recently-used-key-instead-of-the-least-recently-used-one/679953>\
**Category:** web dev\
**Created:** [April 3, 2026, 11:00pm UTC](https://forum.kirupa.com/t/why-does-this-lru-cache-evict-the-most-recently-used-key-instead-of-the-least-recently-used-one/679953 "2026-04-03T23:00:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [April 3, 2026, 11:00pm UTC](https://forum.kirupa.com/t/why-does-this-lru-cache-evict-the-most-recently-used-key-instead-of-the-least-recently-used-one/679953/1 "2026-04-03T23:00:10Z")

</div>

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?

```js
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

---

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [April 3, 2026, 11:07pm UTC](https://forum.kirupa.com/t/why-does-this-lru-cache-evict-the-most-recently-used-key-instead-of-the-least-recently-used-one/679953/2 "2026-04-03T23:07:05Z")

</div>

`Map#set` updates a value but does not move an existing key to the end, so your `get` never refreshes recency.

Yoshiii
