3 ms·
> Unfortunately, this only works for functions of one argument, because each WeakMap key must be one object. If you wish to memoize (x, y) => {...}, you'd need
by laputan_machine 5y ago
> Unfortunately, this only works for functions of one argument, because each WeakMap key must be one object. If you wish to memoize (x, y) => {...}, you'd need a WeakMap whose keys are xs, and whose values are WeakMaps whose keys are ys. This would only work well if y changes frequently but x does not.
Can someone explain why we couldn't concat / hash the incoming arguments and use those as the cache key? My understanding is that the cache key needs to be unique for its inputs, e.g. slow(1, 2) and slow(2, 1) would still give unique key values.
Edit: nevermind I think this is due to the core concept of WeakMaps, which I've never encountered, from the article:
> This is a key/value map that can hold data, but which does not own its keys. This means any record inside will be garbage collected unless another part of the program is also holding on to the same key.
- deleted 5y ago[deleted]