set02

Cache hit check before a slow API call

2 / 26
read the snippet · pick its Big-O
// Avoids re-fetching a user we've already seen.
const cache = new Map();
function getCached(userId) {
if (cache.has(userId)) return cache.get(userId);
return null;
}