Last active
February 11, 2019 03:33
-
-
Save Tauka/b6fa90dbfe2a592da5ff2fe1ee9397b6 to your computer and use it in GitHub Desktop.
Elegant way to memoize js objects
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
// 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