MDN self-hosted library · e01.org
Memoization / notebook

Memoization

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

js
const argToResult = new Map();

function memoizedFunction(arg) {
  if (argToResult.has(arg)) {
    return argToResult.get(arg);
  }
  const result = heavyComputation(arg);
  argToResult.set(arg, result);
  return result;
}