This page looks best with JavaScript enabled

Taking a Look at Groovy @Memoized Functions

 ·   ·  ☕ 2 min read

Reading up on some of the features of Groovy I stumbled upon the @Memoized annotation. This method annotation allows you to tell Groovy to memoize the results from a function. What is memoizing? Think of it as caching the results of a complex computation.

For example the Fibonacci sequence requires us to recursively calculate f(n) = f(n - 1) + f(n - 2). In order to calculate fibonacci for f(100) this means we need to calculate f(1) a hundred times! Why? The answer is going to be the same every time. The idea of memoizing is to take the results of the complex function and store the results after the first computation into a data structure that makes it easier to lookup. For fibonacci this might be as simple as an array with the results of f(n) stored at index n in our array. The general idea being to replace a complex or slow function with a relatively quick table lookup.

In Groovy we get memoization for free as a feature of the language. By adding @Memoized to a function we can tell Groovy to cache the results of a function for the given inputs and use that cache when the same inputs are used again. In our tests we dropped the time of computing the 40th fibonacci number from over 7 seconds (for 20 computations) to 60 milliseconds. An increase of more than 100 times! And it only took us a single line of code to get this advantage too. That’s a lot of power for a single line of code.

The code from this video is available on GitHub here:

More on Groovy at

The Groovy documentation on @Memoized, including help on managing the backing cache and additional examples:

Wikipedia’s article on Time Complexity and what it means (don’t let some of the more obscure terms intimidate you, focus on constant, logarithmic, linear, exponential and factorial time complexity):

The World of Zero Discord channel:

Sam Wronski
Sam Wronski
Maker of things and professional software engineer. Lets make something awesome together!