Skip to content

Instantly share code, notes, and snippets.

@pcreux
Last active June 19, 2023 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcreux/63a76bf36fc13525fc49bba191d4a88c to your computer and use it in GitHub Desktop.
Save pcreux/63a76bf36fc13525fc49bba191d4a88c to your computer and use it in GitHub Desktop.
Benchmark ActiveModel vs Dry::Struct (with strict types)
require 'active_model'
require 'dry-struct'
require 'benchmark/ips'
require 'benchmark/memory'
class AMUser
include ActiveModel::Model
include ActiveModel::Attributes
attribute :id, :integer
attribute :name, :string
attribute :birthday, :date
attribute :last_logged_in_at, :datetime
end
class DryUser < Dry::Struct
module Types
include Dry::Types()
end
attribute :id, Types::Integer
attribute :name, Types::String
attribute :birthday, Types::Date
attribute :last_logged_in_at, Types::Time
end
attributes = { id: 1, name: "Yuki", birthday: Date.new(1987, 12, 30), last_logged_in_at: Time.now }
Benchmark.ips do |x|
x.report("ActiveModel") { AMUser.new(attributes) }
x.report("DryStruct") { DryUser.new(attributes) }
end
Benchmark.memory do |x|
x.report("ActiveModel") { AMUser.new(attributes) }
x.report("DryStruct") { DryUser.new(attributes) }
end
Warming up --------------------------------------
ActiveModel 11.473k i/100ms
DryStruct 26.361k i/100ms
Calculating -------------------------------------
ActiveModel 106.109k (± 6.9%) i/s - 539.231k in 5.107054s
DryStruct 362.653k (± 6.4%) i/s - 1.819M in 5.040403s
Calculating -------------------------------------
ActiveModel 1,192 memsize ( 0.000 retained)
20 objects ( 0.000 retained)
8 strings ( 0.000 retained)
DryStruct 208 memsize ( 0.000 retained)
2 objects ( 0.000 retained)
0 strings ( 0.000 retained)
@cpb
Copy link

cpb commented Jun 19, 2023

As of 2023-06-19 the performance is relatively the same still. ActiveModel 6.1 and ActiveModel 7.0 have similar results:

ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [arm64-darwin22]
Warming up --------------------------------------
         ActiveModel    20.912k i/100ms
           DryStruct    51.259k i/100ms
Calculating -------------------------------------
         ActiveModel    209.278k (± 0.7%) i/s -      1.067M in   5.096391s
           DryStruct    511.021k (± 0.8%) i/s -      2.563M in   5.015644s

Comparison:
           DryStruct:   511020.6 i/s
         ActiveModel:   209278.1 i/s - 2.44x  slower

Calculating -------------------------------------
         ActiveModel     1.024k memsize (   824.000  retained)
                        16.000  objects (    11.000  retained)
                         4.000  strings (     0.000  retained)
           DryStruct   208.000  memsize (     0.000  retained)
                         2.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)

Comparison:
           DryStruct:        208 allocated
         ActiveModel:       1024 allocated - 4.92x more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment