Cache results in memory

Do not ask for the same data twice.

AJAX · Cache results in memoryIntermediate
const cache = new Map();

async function cachedJSON(url) {
  if (cache.has(url)) return cache.get(url);
  const promise = fetch(url).then(r => r.json());      // store the promise: even parallel calls share it
  cache.set(url, promise);
  try { return await promise; }
  catch (e) { cache.delete(url); throw e; }
}
cacheMapperformancefetch