<<-INTRO Question: How fast are these simple use case of memoisation hash = {} hash[:name] = "Peter" hash[:age] = 23 hash[:hair_colour] = "black" hash {}.tap do |hash| hash[:name] = "Peter" hash[:age] = 23 hash[:hair_colour] = "black" end Answer: Not really user system total real local variable 14.170000 0.060000 14.230000 ( 14.223995) tap 15.440000 0.040000 15.480000 ( 15.492474) INTRO require 'memoist' class Foo extend Memoist def memoized 42 end memoize :memoized def memoized_manually @memoized_manually ||= 42 end def memoized_manually_with_defined unless defined?(@memoized_manually_with_defined) @memoized_manually_with_defined = 42 end @memoized_manually_with_defined end def raw 42 end def memoized_with_arg(arg) 42 end memoize :memoized_with_arg end require 'benchmark' TIMES = 10_000_000 Benchmark.bm(40) do |x| x.report "calling memoized method" do foo = Foo.new TIMES.times do foo.memoized end end x.report "calling manually memoized method" do foo = Foo.new TIMES.times do foo.memoized_manually end end x.report "calling manually memoized (with defined?) method" do foo = Foo.new TIMES.times do foo.memoized_manually_with_defined end end x.report "calling non-memoized method" do foo = Foo.new TIMES.times do foo.raw end end x.report "calling memoized method - one time" do TIMES.times do foo = Foo.new foo.memoized end end x.report "calling manually memoized method - one time" do TIMES.times do foo = Foo.new foo.memoized_manually end end x.report "calling manually memoized (with defined?) method - one time" do TIMES.times do foo = Foo.new foo.memoized_manually_with_defined end end x.report "calling non-memoized method - one time" do TIMES.times do foo = Foo.new foo.raw end end x.report "re-loading memoized method" do foo = Foo.new TIMES.times do foo.memoized(true) end end comment = <<-COMMENT x.report "calling memoized method with args" do foo = Foo.new TIMES.times do foo.memoized_with_arg(1337) end end x.report "calling memoized method with args - no cache" do foo = Foo.new TIMES.times do |i| foo.memoized_with_arg(i) end end COMMENT end