This word is a bit more intimidating, surely its the spelling or the how would you pronounce it? In any case, it’s actually a super helpful concept that you (yes, you) can employ to help speed up code. As a base you may see memo, which lines up with what memoization does.
Memoization is a technique used to speed up programs by storing the results and returning a stored result when the same inputs happens.
Case in point, the Classic Fibonacci Sequence
Now this is the recursive version of the sequence, which can be helpful. Unfortunately, as you progress higher with the n input is the recursion can get deeper. Hence, we need to find a way to help cut runtime of our code outputting the n number in the sequence.
First, we can insert an empty object named memo which we will make use of in a second. Use of the memo object lies in using it as base case to pause our recursion, as out function spirals into madness =)
Hence, should N match a key in our memo later on, we will automatically produce the value needed for our input.
So we need to store the current result to our memo, by adjusting our return as such:
Now, our original return/recursion is stored as a key/value of memo[n]. We also make sure to send the memo object into our arguments. Finally, we return memo[n] which is our result of fib(n-1, memo) + fib(n-2, memo).
I do think the most impressive part is the immediate pop when running the function at higher N values. It’s like night and day, and definitely worth the remodel of the original sequence
I’d definitely recommend giving it try, both in this function and the original. Might be great to add to the peskier time consuming functions, you might be struggling with.
Well till next time =D