Sha256: fe46a8a9dd77efce2779e9c0f79e94d5917d7436daf021d02018e0be4466b807

Contents?: true

Size: 912 Bytes

Versions: 11

Compression:

Stored size: 912 Bytes

Contents

require 'dry/struct'

require 'active_record'
require 'benchmark/ips'

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')

ActiveRecord::Schema.define do
  create_table :users do |table|
    table.column :name, :string
    table.column :age, :integer
  end
end

class ARUser < ActiveRecord::Base
  self.table_name = :users
end

module Types
  include Dry::Types.module
end

class DryStructUser < Dry::Struct
  attribute :id, Types::Form::Int
  attribute :name, Types::Strict::String.constrained(size: 3..64)
  attribute :age, Types::Form::Int.constrained(gt: 18)
end

puts ARUser.new(id: 1, name: 'Jane', age: '21').inspect
puts DryStructUser.new(id: 1, name: 'Jane', age: '21').inspect

Benchmark.ips do |x|
  x.report('active record') { ARUser.new(id: 1, name: 'Jane', age: '21') }
  x.report('dry-struct') { DryStructUser.new(id: 1, name: 'Jane', age: '21') }

  x.compare!
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
dry-struct-0.6.0 benchmarks/constrained.rb
dry-struct-0.5.1 benchmarks/constrained.rb
dry-struct-0.5.0 benchmarks/constrained.rb
dry-struct-0.4.0 benchmarks/constrained.rb
dry-struct-0.3.1 benchmarks/constrained.rb
dry-struct-0.3.0 benchmarks/constrained.rb
dry-struct-0.2.1 benchmarks/constrained.rb
dry-struct-0.2.0 benchmarks/constrained.rb
dry-struct-0.1.1 benchmarks/constrained.rb
dry-struct-0.1.0 benchmarks/constrained.rb
dry-struct-0.0.1 benchmarks/constrained.rb