# Spot the bug - #27

**URL:** <https://forum.kirupa.com/t/spot-the-bug-27/681539>\
**Category:** web dev\
**Created:** [May 16, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-27/681539 "2026-05-16T07:00:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Post date:** [May 16, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-27/681539/1 "2026-05-16T07:00:08Z")

</div>

There is one deliberate bug here.

```js
const cache = new Map();

function getUser(id) {
  if (cache[id]) return cache[id];
  const user = { id, name: 'Ari' };
  cache[id] = user;
  return user;
}

```

Reply with what is broken and how you would fix it.

---

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [May 17, 2026, 7:20am UTC](https://forum.kirupa.com/t/spot-the-bug-27/681539/2 "2026-05-17T07:20:23Z")

</div>

`cache` is a `Map`, but the code is treating it like a plain object (`cache[id]`), so you’re not actually using the Map storage at all. The fix is to use the Map API:

```auto
const cache = new Map();

function getUser(id) {
  if (cache.has(id)) return cache.get(id);
  const user = { id, name: 'Ari' };
  cache.set(id, user);
  return user;
}

```

Why it matters: `cache[id]` just sets a normal property on the Map object, which can behave weirdly and won’t participate in Map semantics (like key types, size, and iteration). I found a related kirupa. com article that can help you go deeper into this topic.
