Last active
May 16, 2026 05:57
-
-
Save ThinaticSystem/76d8c0eb0db0a6fbb26db97a3523b245 to your computer and use it in GitHub Desktop.
memoize.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // @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); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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