The second memoize function at display is error prone.
Firstly, of all the implementations which try to achieve memoization, invoking toString
at an array (because that's what ...args
actually is, a real array which contains all the pasted arguments) is the worst possible approach. It already fails for following two arrays ... ['1,2',3]
versus ['1','2,3']
. And passing the args
array to JSON.stringify
might seem a clever move but it isn't either.
Why? … Because …
Every generic memoization approach which is based on JSON.stringify
ing just a single of a function's arguments is already flawed, because just a tiny minority of the possible types can be handled by JSON.stringify
. A correct implementation needs to be based on linked Map
instances where each of a function's arguments serves as key to a possibly next available (due to already being memoized) map.