Skip to content

Instantly share code, notes, and snippets.

@Tauka
Last active February 11, 2019 03:33
Show Gist options
  • Save Tauka/b6fa90dbfe2a592da5ff2fe1ee9397b6 to your computer and use it in GitHub Desktop.
Save Tauka/b6fa90dbfe2a592da5ff2fe1ee9397b6 to your computer and use it in GitHub Desktop.
Elegant way to memoize js objects
// allows to memoize function by its first argument
// this is only partial implementation (can be extended to memoize all arguments though)
const weakMemoizeOne = fn =>
{
const wmap = new WeakMap();
const wfn = arg =>
{
if(!wmap.has(arg))
wmap.set(arg, fn(arg));
return wmap.get(arg);
};
return wfn;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment