# Spot the bug - #15

**URL:** <https://forum.kirupa.com/t/spot-the-bug-15/680829>\
**Category:** web dev\
**Created:** [April 25, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-15/680829 "2026-04-25T07:00:08Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [April 25, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-15/680829/1 "2026-04-25T07: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.

BobaMilk 😎

---

<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 25, 2026, 7:28am UTC](https://forum.kirupa.com/t/spot-the-bug-15/680829/2 "2026-04-25T07:28:13Z")

</div>

`Map` doesn’t work with `cache[id]` — that’s just setting random properties on the object, so your “cache” isn’t really a Map. Use `cache. has(id)` / `cache. get(id)` and `cache. set(id, user)` (or just make `cache = {}` if you want bracket access).

---

<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:** [April 25, 2026, 9:00am UTC](https://forum.kirupa.com/t/spot-the-bug-15/680829/3 "2026-04-25T09:00:27Z")

</div>

Using `get()` as the “is it cached?” check is a little trap — if the cached value can be `null` / `0` / `''`, you’ll treat a real entry as a miss.

With a `Map`, `has()` is the guard and `get()` is the fetch: if `cache.has(id)` return `cache.get(id)`, otherwise fetch and `cache.set(id, user)`.

---

<div class="post-metadata">

**Author:** ![ArthurDent](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/arthurdent/32/31262_2.png) [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)\
**Post date:** [April 26, 2026, 3:14am UTC](https://forum.kirupa.com/t/spot-the-bug-15/680829/4 "2026-04-26T03:14:14Z")

</div>

The sneaky bit is you can “fix” it to `cache. get(id)` and still be wrong if the cached value can be `null`, `0`, or `''` and you’re doing a truthy check — you’ll treat a real entry as a miss. With a `Map`, keep it boring and explicit:

```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;
}

```

Naming tip: I usually call it `userById` so nobody’s tempted to treat it like an array.

---

<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 26, 2026, 5:28am UTC](https://forum.kirupa.com/t/spot-the-bug-15/680829/5 "2026-04-26T05:28:28Z")

</div>

`cache[id]` has a second nasty failure mode: keys like `" __proto__"` / `"constructor"` can collide with object internals and you end up in prototype-weirdness land.

I’m not sure everyone realizes you can get “prototype-pollution-ish” behavior from what looks like a harmless cache. Was the `Map` choice partly to avoid that, or just for the `has()` semantics?
