Sha256: 2f258d9de02b55367540b9d341b6ff2e1b487de05d8b6d076e38b5c8dd8ef45d

Contents?: true

Size: 914 Bytes

Versions: 6

Compression:

Stored size: 914 Bytes

Contents

require 'dry/types'

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 DryTypesUser < Dry::Types::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 DryTypesUser.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-types') { DryTypesUser.new(id: 1, name: 'Jane', age: '21') }

  x.compare!
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dry-types-0.8.1 benchmarks/constrained.rb
dry-types-0.8.0 benchmarks/constrained.rb
dry-types-0.7.2 benchmarks/constrained.rb
dry-types-0.7.1 benchmarks/constrained.rb
dry-types-0.7.0 benchmarks/constrained.rb
dry-types-0.6.0 benchmarks/constrained.rb