I have a situation in which I would greatly benefit from the use of function caching (memoization).
However, my function takes a single argument, whose value is a very large and multi-dimensional Array
.
The standard way to do caching ( and the only way that I can think of) is to create a cache
property on the function itself (cache
is a hash). Each run of the function, you can check for the existence of myFunc.cache[arg]
and simply return that value if it exists, otherwise you perform the calculations as normal and add the argument as a property of cache
afterwards.
However, it appears that javascript does not try to evaluate the strings used as hash keys when you are creating them and always just treats them as strings. example
I could apply JSON.stringify
to the argument, but because the array will large and nested, I am wondering if there is a more efficient way to identify unique arguments.