Facebook iconOptimize Your Rails Project with Memoization and “||=”
Blogs/Technology

Optimize Your Rails Project with Memoization and “||=”

Oct 4, 20243 Min Read
Written by Piyush Duragkar
Optimize Your Rails Project with Memoization and “||=” Hero

Memoization is an optimization technique that can significantly improve the result of expensive computation by caching the data. This technique works perfectly for Ruby on Rails applications. It includes caching the output of complex computing methods and returning it when the same input is entered again.

The ||= operator is a frequently used technique in Ruby memoization implementation.

Performance optimization is essential for Ruby on Rails applications, particularly when handling costly computations and a high volume of database queries. Memoization is a strategy that, by caching the outcomes of costly operations, can greatly increase performance. Using the ||= operator is one of the easiest ways to do memoization in Ruby.

Example

Let's take an example for calculating the factorial of a number. Factorial is denoted as (!n). It is calculated as the product of all positive integers less than or equal to n.

class Factorial

  def calculate(n)
   return 1 if n<=1
    n * calculate(n - 1)
  end
end

fact = Factorial.new
puts fact.calculate(5)
#output = 120

This is the example for calculating the factorial of a number. In this, each call to the calculation method leads to unnecessary calculation.

Partner with Us for Success

Experience seamless collaboration and exceptional results.

Challenges

The primary difficulty in this case is doing the same calculations again, which could cause impactful performance issues for greater values of n.

The solution for the above problem is Memoization with ‘||=’

Memoization is an approach where you actually save the result of a method that is being called repeatedly and then reuse the result of that stored method when that method is called again. The ‘||=’ operator in Ruby provides a simple way to implement memoization.

||=’ this operator is shorthand for “or equals”. It assigns a value to a variable only if that variable is nil or false.

Example

With Memoization

class Factorial

  def initialize
     @value = {}
  end
  
  def calculate(n)
    return 1 if n <= 1
    if @value[n]
      puts "Returning from memoized value for #{n}!"
      return @value[n]
    end
    puts "Calculating factorial for #{n}"
    @value[n] ||= n * calculate(n - 1)
  end
end

fact = Factorial.new
puts fact.calculate(5)

#output
puts fact.calculate(5)
Calculating factorial for 5
Calculating factorial for 4
Calculating factorial for 3
Calculating factorial for 2
120

puts fact.calculate(7)
Calculating factorial for 7
Calculating factorial for 6
Returning from memoized value for 5!
5040

puts fact.calculate(5)
Returning from memoized value for 5!
120

puts fact.calculate(3)
Returning from memoized value for 3!
6

In this example, the calculate method uses memoization with this  ‘||=’ operator. The purpose of this operator is to check if the factorial for the given number is already present or not in the @value hash, if present then returns the prints and return value. If you first calculate 5! Then 7! Then memoization will use 5! Result for calculation, which is more efficient. 

The above example prints the result with a memoized value.

Partner with Us for Success

Experience seamless collaboration and exceptional results.

Tips for Effective Memoization

  • Use memoization for the methods that do complex computation or database queries and that function is being called frequently with the same input or slight change in the input. For instance, operations involving complex calculations, database queries, or API calls can benefit greatly from memoization.
  • Ensure that the methods return the same result for the same inputs.
  • Memoization stores the results in the memory. Which can lead to high memory usage if not managed properly. If a method has multiple possible inputs, then ensure that cache results do not take excessive memory. Consider limiting cache size.
  • Memoization works best when the input is simple and immutable. 
  • Memoize at the lowest possible level of abstraction where the expensive operation occurs. Memoizing high-level methods might cache more data than necessary, leading to inefficiency.
  • Document why and where memoization is being used in your code. This helps other developers understand the purpose and potential pitfalls, like cache invalidation scenarios.

Conclusion

Memoization with the ‘||=’  operator is a simple and powerful technique to optimize the performance of your applications. By caching the results of expensive operations, you can significantly reduce the computational overhead and improve the efficiency of your application.

Frequently Asked Questions?

1. What is memoization in Ruby?

Memoization is an optimization technique that caches expensive computation results, returning stored values for repeated inputs to improve performance.

2. How does the ||= operator work in Ruby memoization?

The ||= operator assigns a value to a variable only if it's nil or false, enabling simple memoization implementation.

3. When should I use memoization in Rails?

Use memoization for frequently called methods with complex computations or database queries that return consistent results for the same inputs.

Author-Piyush Duragkar
Piyush Duragkar

Backend Developer with 3+years of experience with Ruby on Rails. Specialising in backend optimisation, API development, and database. Enthusiastic about developing scalable, effective solutions.

Phone

Next for you

5 Rust Features I Wish JavaScript and Ruby Had Cover

Technology

Jan 22, 20259 min read

5 Rust Features I Wish JavaScript and Ruby Had

Recently, I have been tinkering with an RPi Zero 2 W board: making it serve a small app on my local network, running a lightweight LLM model to see how long it takes it to respond to a prompt (16 minutes 😢), and just the kind of experiments that you would run on a microcomputer of that scale. Being a full-stack developer, I wondered if I could use such a low-spec microcomputer as a server for a small internal tool. My first thought was to create that app in Ruby on Rails, and my second thought

New React 19 Features You Shouldn’t Miss Out Cover

Technology

Jan 22, 20254 min read

New React 19 Features You Shouldn’t Miss Out

Struggling with complex React development challenges in 2025? As a front-end developer who's been in the React trenches for years, I'm thrilled about React 19's release. With innovative features designed to streamline common pain points, this update promises to transform how we build web applications. The features introduced in this version tackle many long-standing challenges developers face daily. While React 19 brings numerous improvements, I want to focus on the React 19 features that will

How to Integrate Google Maps into Your Flutter App Cover

Technology

Nov 20, 20244 min read

How to Integrate Google Maps into Your Flutter App

In our smartphone-driven world, location services are transforming the app landscape. The global location-based services market is projected to hit a staggering reach USD 129.71 billion by 2032, exhibiting a CAGR of 19.5% during the forecast period (2024-2032) This explosion stems from our smartphone addiction and hunger for personalized experiences. It's only natural that developers are rushing to integrate Google Maps into their apps, aiming to deliver an engaging, user-friendly mapping experi