Saving our work with memoization
We're going to look at a simple technique we can use to avoid repeating work. This does not take the place of more advanced performance optimization, but it is a nice way to do some common-sense improvement with almost no effort on your part. The general idea, known as memoization, is to store calculated information so that we can quickly look it up later rather than recalculating it. Now that our application is using classes, we are dealing with objects, and objects have state that we can exploit to easily store modest amounts of data.
There are more complicated (and powerful) ways to memoize data, but we're going to keep it simple. When a piece of information is requested, we'll see if it's stored. If so, we return it immediately. If not, we calculate it and store the answer for later use. This technique is most helpful when we need to do some resource-intensive computation to receive an answer, or when we must use a slow channel such as network requests...