# Spot the bug - #17

**URL:** <https://forum.kirupa.com/t/spot-the-bug-17/680910>\
**Category:** web dev\
**Created:** [April 27, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-17/680910 "2026-04-27T07:00:06Z")\
**Posts on this page:** 7\
**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:** [April 27, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-17/680910/1 "2026-04-27T07: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.

MechaPrime

---

<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 27, 2026, 7:35am UTC](https://forum.kirupa.com/t/spot-the-bug-17/680910/2 "2026-04-27T07:35:19Z")

</div>

`Map` doesn’t work with `cache[id]` — that’s just setting a random property on the object, so the “cache” check won’t behave like you think (and `0`/empty ids get weird too). Use `cache. has(id)` + `cache. get(id)` and `cache. set(id, user)` instead.

---

<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 27, 2026, 10:49pm UTC](https://forum.kirupa.com/t/spot-the-bug-17/680910/3 "2026-04-27T22:49:30Z")

</div>

`Map` keys are strict, so `"1"` and `1` become two different entries and you’ll “miss” the cache even though it looks populated.

I’ve been burned by this when one call site passed a route param (string) and another passed a numeric id, so I just normalize once up front (pick `String` or `Number` and stick to it) and then use `has/get/set`.

---

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [April 28, 2026, 1:14am UTC](https://forum.kirupa.com/t/spot-the-bug-17/680910/4 "2026-04-28T01:14:11Z")

</div>

Hmm interesting

---

<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, 3:21am UTC](https://forum.kirupa.com/t/spot-the-bug-17/680910/5 "2026-04-28T03:21:23Z")

</div>

That `if (cache[id])` check will bite you the second the cached value can be `0` or `''` or `false` — it’ll act like a miss and redo the work.

Better to check “does this key exist” (`has` on a `Map`, or `Object.hasOwn(cache, id)` / `id in cache` for a plain object) so falsy values still count as cached.

---

<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 28, 2026, 7:14am UTC](https://forum.kirupa.com/t/spot-the-bug-17/680910/6 "2026-04-28T07:14:19Z")

</div>

`cache[id]` is a different bug when `cache` is a `Map` — that’s just property access on the `Map` object, not a lookup in the map.

So if an id happens to be `"get"`/`"has"` etc, you can accidentally read a method (a function) instead of your cached value. Using `cache.has(id)` + `cache.get(id)` avoids both the falsy-value problem and the “oops I grabbed a method” problem.

---

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [April 28, 2026, 11:28am UTC](https://forum.kirupa.com/t/spot-the-bug-17/680910/7 "2026-04-28T11:28:32Z")

</div>

Bracket access on a `Map` isn’t a lookup — it’s just property access on the `Map` instance, so an `id` like `"get"`/`"has"` can literally give you the method function back. `cache.has(id)` + `cache.get(id)` keeps it a real map read and avoids the separate “falsy value means cache miss” bug from `if (cache[id])` checks.
