Skip to content

Instantly share code, notes, and snippets.

@ThinaticSystem
Last active May 16, 2026 05:57
Show Gist options
  • Select an option

  • Save ThinaticSystem/76d8c0eb0db0a6fbb26db97a3523b245 to your computer and use it in GitHub Desktop.

Select an option

Save ThinaticSystem/76d8c0eb0db0a6fbb26db97a3523b245 to your computer and use it in GitHub Desktop.
memoize.js
// @ts-check
"use strict";
const INITIAL = Symbol("INITIAL");
/**
* @template T
* @param {() => T} computation
*/
export const memoize = (computation) => {
/** @type {typeof INITIAL | T} */ let cache = INITIAL;
return () => (cache === INITIAL ? (cache = computation()) : cache);
};
const INITIAL = Symbol("INITIAL");
export const memoize = <T>(computation: () => T) => {
let cache: typeof INITIAL | T = INITIAL;
return () => (cache === INITIAL ? (cache = computation()) : cache);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment