Defining Memoization Concept 0:00Wikipedia tells us that memoization is a technique used to speed up programs by storing the results of expensive function calls and returning the cast result when the same inputs occur again. Now, I don't know about you, but often these definitions confuse me, and I just want to see an example. So here you go. Here I have a trait called Progressible that I can attach to any series. It allows me to do things like this. For the current series, give me the user's progress. So for example, what's the next episode the user should work on? Progress Trait Use Case 0:28For the current series, give me the user's progress. So for example, what's the next episode the user should work on? What is their completion ratio? How many more lessons do they have to watch? Things like that. It's useful. But calculating all of that is actually a little bit costly. It's one of those kind of clunky SQL queries. So when we call the progress method, it's delegating to a query class called SeriesProgress. Now you don't need to look at this at all. Identifying Query Performance Issue 0:48So when we call the progress method, it's delegating to a query class called SeriesProgress. Now you don't need to look at this at all. It's one of those kind of nasty SQL queries that you tuck away and then you never have to worry about again. Most projects have them. But anyways, because that's so costly, we do have one issue. Every single time I call this method, it is running that query. Or at the very least, it's running the query and caching it and checking if it's in the cache on future iterations. It's still somewhat costly.cache on future iterations. It's still somewhat costly. So that means if on any given page, I call SeriesProgress in multiple places, for example, a completion rate or something like that, if I do that in multiple places, well, it's not very performant. So one way to clean this up is to use memoization. Again, a fancy term that's really simple when you think about it. One more time, a technique used to speed up programs by storing the results of expensive function calls. Okay.function calls. Okay. Again, when we say expensive, we're just talking about things that take a long time. So we're going to store the results. Okay. Store the results. So we have something like that. And now returning the cached result when the same input occurs again. So that means when I call the method, we need to check, well, has this been cached already? All right. Implementing Memoization Pattern 2:05So that means when I call the method, we need to check, well, has this been cached already? All right. It sounds like it should be cached on the instance. So here I could say, well, if it has not been cached yet, so if progress is falsy, then it's the first time we called the method. So run the query and then store the results. Like so. Finally, return. So we can do this number here. And that's it.So we can do this number here. And that's it. That's all there is to it. That's memoization in effect. So think about what we now have. When this method is called, we check, well, have we cached the progress for the series yet? On the first call, no. All right. We'll do this expensive query and then cache it. Explaining Cached Property Behavior 2:45All right. We'll do this expensive query and then cache it. And again, when we say cache, sometimes that can be confusing. In this case, we're just saying save it to a property. That's it. Store it somewhere for future use and then return it. Okay. Now what about the next time we call the method? Is progress falsy? No.Is progress falsy? No. That's equal to the results of that expensive query. All right. Well, don't do anything here. Just return it because we've already performed that call. So once again, this is memoization in effect. I think you'll find, like many things in programming, the term itself is far more scary sounding than the implementation. And in fact, I bet you've even been using this pattern without even knowing that itsounding than the implementation. And in fact, I bet you've even been using this pattern without even knowing that it had a name.