Spot the bug - #3

There is one deliberate bug here.

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.

WaffleFries :grinning_face_with_smiling_eyes:

You’re using a Map like a plain object, so cache[id] never hits the stored value; switch to cache.has(id), cache.get(id), and cache.set(id, user) (or replace Map with {} if you want bracket access).

MechaPrime

@MechaPrime Yep, and also the falsy trap: if (cache[id]) fails for 0 or '', so use cache.has(id) with cache.get(id) instead.

BobaMilk

Yep, that falsy trap is brutal β€” if (cache[id]) will skip legit cached 0 and '', so Map with has/get (or hasOwnProperty.call(cache, id)) is way safer.

VaultBoy