Learning about Memoization

John Daise
johnsthoughts
Published in
2 min readJul 18, 2018

While going through the pre-work for bootcamp, I came upon an exercise where had to build a method that required a particular function to run only once. Within that method there had to be a check to see whether said function had already been performed. If that function had already been performed, the method would skip running the step of running that particular function again. With my limited knowledge I decided to consult Google for answers. In my search I came across a symbol “||=” (“or-equals”). I immediately used this operator and magically my code worked! But what really is happening here…

What is “||=”??

“||=” or “or-equals” is a special operator that allows a programmer to perform a technique called memoization.

What is Memoization? Why is it useful?

Memoization is a technique that allows a computer to process more efficiently. There are certain processes that are considered to be “expensive” (processes that would be a lot of work for the computer to perform). Anytime you are dealing with “expensive” code, this may be a situation where memoization could be useful. More specifically one can take any previously computed results to be stored and used again later rather than repeating a particular computation over and over again.

How does “||=” (“or-equals”) work?

Example:

x ||= y

Let’s look at a basic example above. So imagine that you have a method and you get to a line that looks like what is written above. Going from left to right, the computer is going to evaluate “x”. If “x” already has a value or is defined, then the computer will go about using x for whatever it is to be used for. If you come to a situation where “x” is undefined, nil or false, then “x” will become equal to “y” (or will be assigned to the value “y”). From my understanding the “y” portion of this line does not have to be a single value but it could also be a larger, more “expensive” block of code that a programmer may not necessarily want the computer to run again.

At this current point in my coding journey, I am simply learning the basics of how to program. From what I know, when anyone is looking to build more efficient code or refactor code, memoization is a technique that one can use. In scenarios where you begin to notice that you are computing a series of smaller functions repeatedly or where the number of computations may grow exponentially, even after recursive methods are employed, memoization may be the solution.

Side thought:

From my understanding there are some mysteries around how “||=” (“or-equals”) can be utilized. Supposedly there are some flaws in using the operator to perform memoization compared to other approaches. I have yet to learn about any of that but if you learn or know anything please let me know.

Suggested Reading:

https://en.wikipedia.org/wiki/Memoization

http://www.rubyinside.com/what-rubys-double-pipe-or-equals-really-does-5488.html

https://allenan.com/ruby-or-equals-operator-default-booleans/

http://gavinmiller.io/2013/basics-of-ruby-memoization/

--

--