Skip to content

Instantly share code, notes, and snippets.

@RodEsp
Created December 12, 2023 22:53
Show Gist options
  • Save RodEsp/8a2a39382d26adff9ae871e3acda1b08 to your computer and use it in GitHub Desktop.
Save RodEsp/8a2a39382d26adff9ae871e3acda1b08 to your computer and use it in GitHub Desktop.
JavaScript Memoize Function
export const memoize = (fn) => {
const cache = new Map();
return (...args) => {
const key = JSON.stringify(args);
if (cache.has(key)) return cache.get(key);
const result = fn(...args);
cache.set(key, result);
return result;
}
}
@RodEsp
Copy link
Author

RodEsp commented Dec 12, 2023

Basic, general-purpose, memoize function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment