Skip to main content

Memoization

Memoization is a technique for performance optimization. It is, essentially, calculating a value once and then caching it within an object for the duration of its use.

Memoization comes from the Latin word โ€œmemorandumโ€, meaning โ€œto be rememberedโ€.

(Sourced from Jason Swettโ€™s post on memoization)

class Product

def price
expensive_calculation
end

def memoized_price
@memoized_price || expensive_calculation
end

def expensive_calculation
sleep 1
500
end
end