Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Benchmarking command object pattern vs PORO
require 'benchmark'
require 'active_support/all'
class CommandObject
def self.execute_with(arg)
new(arg).tap(&:run)
end
def initialize(arg)
@arg = arg
end
def run
@ran = true
end
end
class PORO
def initialize(arg)
@arg = arg
@ran = true
end
end
def a_method(arg)
@arg = arg
@ran = true
end
n = 100_000
Benchmark.bmbm(15) do |x|
x.report("command-object") { n.times { CommandObject.execute_with("thing") } }
x.report("PORO") { n.times { PORO.new("thing") } }
x.report("method") { n.times { a_method("thing") } }
end
# >> user system total real
# >> command-object 0.028089 0.000126 0.028215 ( 0.028239)
# >> PORO 0.016680 0.000005 0.016685 ( 0.016683)
# >> method 0.007609 0.000002 0.007611 ( 0.007611)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment