In the never-ending quest to write code that is performant, we have many techniques at our disposal. One of those techniques is memoization,11 1 That’s memoization, not memorization — there’s no “r”! which boils down to storing the results of expensive function calls, so that these expensive functions do not need to be called more than absolutely necessary.
Many years ago, I wrote a Ruby gem for memoization, ddmemoize. It has since been superseded by better solutions, but for a long time, it was one of the best memoization libraries for Ruby out there.
Creating this library taught me a great deal. Memoization is surprisingly complex, and a proper implementation, it turns out, goes far beyond Ruby’s ||= memoization operator. Memory management and thread safety, for example, are important considerations, though often overlooked.
The simplest tool at our disposal for remembering values is the concept of local variables. Trivial, perhaps — but still worth mentioning.