Last active
June 19, 2023 22:37
-
-
Save pcreux/63a76bf36fc13525fc49bba191d4a88c to your computer and use it in GitHub Desktop.
Benchmark ActiveModel vs Dry::Struct (with strict types)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As of 2023-06-19 the performance is relatively the same still. ActiveModel 6.1 and ActiveModel 7.0 have similar results: