Transcript from the "Caching with Memoization" Lesson
>> Bianca: We were talking about using the breadcrumbs method to cache values that we've seen before to optimize our unique function. Memoization is a type of caching, as well, and so the difference between memoization and the breadcrumbs method is that the value that we're getting to cache, so we rooted array at i and saved that into our breadcrumbs object, is not the result of a function.
[00:00:57] Or even a variable, more like an object and array actually. That's all caching, so if you refresh the page, that's gonna be erased, it's not saved on discs, that's caching. Same with Node, if you're gonna cache it you're saving it into an object that's just on the server.
[00:01:36] Anyway, we're gonna talk about factorial. So, factorial is the one were it's like, like n to the bang, bang is the exclamation point. That's how we say it in programming speak, bang. So what that looks like, so 5 factorial is 5 with a bang, which is just 5 x 4 x 3 x 2 x 1.
>> Bianca: Cool. So with factorial, we're doing a lot of the same calculations over and over and over again. And so instead of re-calculating the result of this, it's actually a recursive function. We could just memoize it, which means we're going to save the results of the factorial function into an object.
[00:02:36] Just like in the breadcrumbs version, except that the value that we're caching is gonna be returned from a function. This is very very similar, just where the value comes from is what differs.
>> Bianca: Cool. And so you can see they're repeated here so it just keeps getting concatenated.
[00:03:08] And so instead of calculating 36 again, if we just save 35, then we don't have to go through all of the decision trees of calculating the recursion of a factorial.
>> Speaker 2: Are you missing some level, and you have sudo code. Is there some kind of code that's supposed to go there, you're just not writing it?
>> Bianca: Yeah.
>> Speaker 2: The assumption is that there's gonna be code there.
>> Bianca: Yeah, there's some code there, it takes away from the point because it's a little confusing. But the point is is that factorial of 36 is just factorial of 35 times 36. So instead of every time we calculate, if we already calculated a factorial of 35, we don't need to recalculate it all over again, right?
[00:03:52] Because I mean you got to do 35 times 34 times 33 all the way down to 1. So, do you have a question? Okay.
>> Speaker 3: When you talk about caching and memoization?
>> Bianca: Mm hm?
>> Speaker 3: Could that relate back to space complexity which you mentioned earlier along the time complexity.
>> Bianca: Yeah.
>> Speaker 3: Is this more an example that it's gonna affect space complexity?
[00:04:44] It's something you want to think about more, but in Java Script world, you can always memoize, always cache. It's always a good idea, unless you are getting into big data stuff then that's something that most of us don't deal with on the day to day.