Skip to content

Instantly share code, notes, and snippets.

@ax2mx
Created July 17, 2022 09:07
Show Gist options
  • Select an option

  • Save ax2mx/6b762f148da1f989aeef6367dcb5dfd4 to your computer and use it in GitHub Desktop.

Select an option

Save ax2mx/6b762f148da1f989aeef6367dcb5dfd4 to your computer and use it in GitHub Desktop.
Simple memoize wrapper
const argKey = x => x.toString() + ':' + typeof(x);
const generateKey = args => args.map(argKey).join('|');
const memoize = fn => {
const cache = Object.create(null);
return (...args) => {
const key = generateKey(args);
const val = cache[key];
if (val) return val;
const res = fn(...args);
cache[key] = res;
return res;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment