We can use the internal workings of functions to remember previously computed values, thus improving the performance of code we write by a huge margin (this is an especially important thing to utilize during algorithm writing/coding interviews).
Memoization
Memoization is a technique that improves the performance of a function by storing the previously computed value in a fast-accessed memory (cache). The result of a function based on certain arguments will be stored together with the arguments inside the cache. The next time we run the function with the same arguments we can return the previously stored result, instead of running the function again. We can skip a lot of complex computation this way, and speed up our functions.
We wrote a simple snippet below. The code calculates and finds a prime number using memoization.
function isPrime(value) { let prime = true; if (!this.answerCollection) { this.answerCollection = {}; } if (this.answerCollection[value] !== undefined) { console.log("We have this value cached!"); return this.answerCollection[value]; } for (let i = 2; i < value; i++) { if (value % i == 0) { prime = false; break; } } return (this.answerCollection[value] = prime); }
First, we check whether we have the answerCollection
already defined. The answerCollection
object is set inside the local scope of the function (using the this
keyword). We will use the answerCollection
object as a cache in which we will store results for arguments that were already calculated in a previous invocation.
We also create the prime
variable that will change if the number is not a prime number.
let prime = true; if (!this.answerCollection) { this.answerCollection = {}; }
Then we check whether the result of the argument we provided has already been cached in the answerCollection
. As mentioned above, we will use the prime variable to store the computed value inside our collection. If we find a cached answer, we will return it. We can run this method multiple times using the same argument, and the second time we try to recalculate the prime number we would get this nice message that says – “We already have this value cached.”
if (this.answerCollection[value] !== undefined) { console.log("We have this value cached!"); return this.answerCollection[value]; }
Final Words
Now memoization seems like a really cool and advantageous concept, right? We generate some major performance benefits by retrieving a previously computed value. Also, the user is not aware of anything going on, as it all happens behind the scenes. Well, we should also mention some disadvantages. We might gain speed but we lose memory, any sort of caching will definitely need to load more memory to compensate for performance gains. And we definitely would find it hard to test memoizated functions, because the provided results are not ‘pure’.
So like always, weigh in on the advantages and disadvantages and decide whether you should use memoization in your methods.
For more articles please click below, or check the blog.
- Largest Palindrome Product
- Largest Prime Factor
- Even Fibonacci Numbers
- Multiples of 3 or 5
- How to find the missing number in a given integer array of 1 to 100?
- Understanding Javascript prototypes
- Difference between Promise.all() and Promise.race()
- JavaScript generators
- Using this and arguments
- Arrow functions in JavaScript