Skip to content

Instantly share code, notes, and snippets.

@rmm5t
Last active June 18, 2019 23:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmm5t/8eb05273217ec9d85ee90592b2c57876 to your computer and use it in GitHub Desktop.
Save rmm5t/8eb05273217ec9d85ee90592b2c57876 to your computer and use it in GitHub Desktop.
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