If you use a constant for your cache, you would still be able to add some keys to it. Plus, it makes reassignment by mistake throw an error which is an added security.
You can even use an immediately invoked function expression to handle to prevent having to call the function to get the closure first.
Plus, your else branch is not necessary since you are returning (and thus stopping the execution of the function) in your conditional statement.
In the end, this proposal could serve as an alternative for implementing the memoized square function.
constsquare=(()=>{constcache={};returnvalue=>{if(typeofcache[value]!=="undefined"){console.log("Fetching value from the cache.");returncache[value];}console.log("Computing and storing the result in the cache.");returncache[value]=value*value;};})();console.log(square(2));console.log(square(4));console.log(square(6));console.log(square(6));// Computing and storing the result in the cache.// 4// Computing and storing the result in the cache.// 16// Computing and storing the result in the cache.// 36// Fetching value from the cache.// 36
And if you need to create multiple memoized functions, you can even extract the memoization in its own helper function.
constmemoize=callback=>{constcache={};return(...parameters)=>{constargs=JSON.stringify(parameters);if(typeofcache[parameters]!=="undefined"){console.log("Fetching value from the cache.");returncache[parameters];}console.log("Computing and storing the result in the cache.");returncache[parameters]=callback(...parameters);};};constsquare=memoize(value=>value*value);console.log(square(2));console.log(square(4));console.log(square(6));console.log(square(6));// Computing and storing the result in the cache.// 4// Computing and storing the result in the cache.// 16// Computing and storing the result in the cache.// 36// Fetching value from the cache.// 36
Thanks for the feedback @aminnairi
. Excellent writeup and valid points. Instead of updating my original post with your recommendation, I am going to add a note to it. Readers can see how I approached it and how you made it better (and the reasoning behind it)
In the case of multiple memorization functions I do however prefer @shadowtime2000
implementation (but that is just a preference based on readability)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi Parwinder and thanks for your article.
If you use a constant for your cache, you would still be able to add some keys to it. Plus, it makes reassignment by mistake throw an error which is an added security.
You can even use an immediately invoked function expression to handle to prevent having to call the function to get the closure first.
Plus, your
else
branch is not necessary since you are returning (and thus stopping the execution of the function) in your conditional statement.In the end, this proposal could serve as an alternative for implementing the memoized square function.
And if you need to create multiple memoized functions, you can even extract the memoization in its own helper function.
You could also use a
Map
for that.Why not just
cache.get(parameters) !== undefined
?👏🏼 I like this
Thanks for the feedback @aminnairi . Excellent writeup and valid points. Instead of updating my original post with your recommendation, I am going to add a note to it. Readers can see how I approached it and how you made it better (and the reasoning behind it)
In the case of multiple memorization functions I do however prefer @shadowtime2000 implementation (but that is just a preference based on readability)