typescript-memoize
typescript-memoize copied to clipboard
Improvement suggestion
Hi, I've seen you are setting a variable directly in the target object:
I can suggest 2 alternative to prevent having possible collisions with another namespace (its very unlikely but still possible)
A. use a Symbols this will completely prevent having name collisions on keys
B. use WeakMaps to store a result using a function as the key (that's probably the holy grail for your implementation). What I can imagine would be: a weak map to a regular map returning itself the cached value
{[callingMethod:Map<hash, any>]}
In this manner, there is no need to add an id to the function and if the function you Memoized does not exist anymore, there is no risk of a memory leak (maybe it's already the case, not sure… I haven't dug yet)
I know this is an old thread, but I went ahead and built a new memoize decorator that utilizes WeakMap under the hood. https://github.com/milesj/boost/blob/master/docs/common.md#memoize