0

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.

Luke
  • 5,047
  • 4
  • 31
  • 58

1 Answers1

0

This question is basically what I was asking without knowing it:

JavaScript Hashmap Equivalent

In my case, the simplest solution is just to manually make the 0th index of my arrays an ID that I can use as the hash key.

Without a means of doing this, or something similar, you have to either create your own hashmap (outlined in the linked question) or wait for the official Map object to be implemented.

Community
  • 1
  • 1
Luke
  • 5,047
  • 4
  • 31
  • 58