# Spot the bug - #18

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

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [April 28, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-18/680951/1 "2026-04-28T07:00:06Z")

</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.

Sarah

---

<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 28, 2026, 9:21am UTC](https://forum.kirupa.com/t/spot-the-bug-18/680951/2 "2026-04-28T09:21:24Z")

</div>

`cache` is a `Map`, but the code is treating it like an object with `cache[id]`, so the “cache hit” path never really uses the Map API.

Fix by either switching to `cache.has(id) / cache.get(id) / cache.set(id, user)`, or change `cache` to `{}` if you actually wanted property access.

---

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [April 28, 2026, 11:14am UTC](https://forum.kirupa.com/t/spot-the-bug-18/680951/3 "2026-04-28T11:14:11Z")

</div>

Yep

---

<div class="post-metadata">

**Author:** ![Quelly](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/quelly/32/31386_2.png) [@Quelly](https://forum.kirupa.com/u/Quelly)\
**Post date:** [April 28, 2026, 1:28pm UTC](https://forum.kirupa.com/t/spot-the-bug-18/680951/4 "2026-04-28T13:28:43Z")

</div>

Changing `cache` to `{}` can bite you hard if `id` is ever an object — all those keys stringify to `"[object Object]"` and you silently overwrite entries.

```auto
const cache = {};
const id1 = { a: 1 };
const id2 = { a: 2 };

cache[id1] = "user1";
cache[id2] = "user2";

console.log(Object.keys(cache)); // ["[object Object]"]
console.log(cache[id1]); // "user2" (oops)

```

`Map` is the boring fix here because it keeps object keys as actual keys, so `has/get/set` behave the way your brain expects.

```auto
const cache = new Map();
cache.set(id1, "user1");
cache.set(id2, "user2");

console.log(cache.has(id1)); // true
console.log(cache.get(id1)); // "user1"

```

---

<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 28, 2026, 8:14pm UTC](https://forum.kirupa.com/t/spot-the-bug-18/680951/5 "2026-04-28T20:14:14Z")

</div>

Square brackets on a `Map` are a trap: `cache[id]` doesn’t touch the map storage at all, it just slaps a normal property onto the `Map` object. So it looks fine in a quick test, then `cache.clear()` does nothing to your “cached” values because they were never in the map in the first place.

---

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [April 28, 2026, 10:14pm UTC](https://forum.kirupa.com/t/spot-the-bug-18/680951/6 "2026-04-28T22:14:33Z")

</div>

Square brackets on a `Map` don’t “sort of work” — they’re just wrong. `cache[id]` is a plain object property, so `cache.clear()` won’t touch it and you get this half-broken cache that lies to you in tests.

The `if (cache.get(id))` thing is a separate landmine though. If you can ever cache falsy values, you’ll recompute forever. Use `has()` for presence and `get()` for the value, like you showed.

---

<div class="post-metadata">

**Author:** ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Post date:** [April 28, 2026, 11:42pm UTC](https://forum.kirupa.com/t/spot-the-bug-18/680951/7 "2026-04-28T23:42:09Z")

</div>

That is clean
