Memoization in Ruby
A quick note on improving Rails performance using Memoization.
Memoization is the process of storing a computed value to avoid duplicated work by future calls.
# originally, will search + retrieve from database
def current_user
User.find(session[:user_id])
end
# now, search + retrieve from memory (fast)
def current_user
# NOTE: this will fail on conditional assignment,
# e.g. the returning value is nil or false.
@current_user ||= User.find(session[:user_id])
end
# now (improved), handle false or nil
def current_user
@current_user = User.find(session[:user_id]) unless defined?(@current_user)
end
# with parameterized methods, use it wisely.
def user(name)
return @users[name] unless @results[name].nil?
@users[name] = User.where(name: name)
end