# Spot the bug - #8

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

<div class="post-metadata">

**Author:** ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Post date:** [April 18, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-8/680582/1 "2026-04-18T07: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.

Ellen

---

<div class="post-metadata">

**Author:** ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)\
**Post date:** [April 18, 2026, 7:21am UTC](https://forum.kirupa.com/t/spot-the-bug-8/680582/2 "2026-04-18T07:21:15Z")

</div>

You’re creating a `Map` but reading/writing with `cache[id]`, which just sticks a random property on the Map object instead of using its key store.

Use `cache.has/get/set` (or switch `cache` to `{}` if you really 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 18, 2026, 8:14am UTC](https://forum.kirupa.com/t/spot-the-bug-8/680582/3 "2026-04-18T08:14:21Z")

</div>

`if (cache[id])` is a bad cache check because a stored `0` or `false` reads like a miss and you recompute.

Since you’re using a `Map`, do `cache.has(id)` and `cache.get(id)` so falsy values still count as hits.

---

<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 18, 2026, 10:28am UTC](https://forum.kirupa.com/t/spot-the-bug-8/680582/4 "2026-04-18T10:28:08Z")

</div>

One more edge case: `cache[id]` coerces keys to strings, so `cache[1]` and `cache["1"]` end up as the same entry, which rather defeats the point of using `Map`.

---

<div class="post-metadata">

**Author:** ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)\
**Post date:** [April 18, 2026, 11:28am UTC](https://forum.kirupa.com/t/spot-the-bug-8/680582/5 "2026-04-18T11:28:15Z")

</div>

`if (cache[id])` trips me up the most, since `0` or `""` gets treated like a miss and you end up rebuilding. A `Map` fixes that with `cache.has(id)`, so falsy values still count as cached and you can `get` them right after.

---

<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 18, 2026, 10:00pm UTC](https://forum.kirupa.com/t/spot-the-bug-8/680582/6 "2026-04-18T22:00:11Z")

</div>

One more weirdness: by doing `cache[id] = user` you can accidentally stomp on real `Map` properties (try `id = "size"` and now `cache. size` is an object), which is a fun way to create a “works until it doesn’t” bug.

---

<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 19, 2026, 2:49am UTC](https://forum.kirupa.com/t/spot-the-bug-8/680582/7 "2026-04-19T02:49:25Z")

</div>

Yeah, that’s the sort of bug that only shows up in prod at 4:55pm on a Friday. If you really want a plain key/value cache, `new Map()` (or `Object. create(null)` if you’re being old-school) saves you from tripping over `size`/`constructor` nonsense.
