Sha256: b4862e8dbec14b56a0208adc62c678b150f95aa64d8de4803312475fa2278c3f

Contents?: true

Size: 1.07 KB

Versions: 15

Compression:

Stored size: 1.07 KB

Contents

$LOAD_PATH.unshift('lib')

require 'bundler/setup'
require 'dry-types'

module SchemaBench
  def self.hash_schema(type)
    Dry::Types['hash'].public_send(type,
      email:   Dry::Types['string'],
      age:     Dry::Types['form.int'],
      admin:   Dry::Types['form.bool'],
      address: Dry::Types['hash'].public_send(type,
        city: Dry::Types['string'],
        street: Dry::Types['string']
      )
    )
  end

  private_class_method(:hash_schema)

  SCHEMAS =
    Dry::Types::Hash
      .public_instance_methods(false)
      .map { |schema_type| [schema_type, hash_schema(schema_type)] }
      .to_h

  INPUT = {
    email: 'jane@doe.org',
    age: '20',
    admin: '1',
    address: { city: 'NYC', street: 'Street 1/2' }
  }
end

require 'benchmark/ips'

Benchmark.ips do |x|
  SchemaBench::SCHEMAS.each do |schema_type, schema|
    x.report("#{schema_type}#call") do
      schema.call(SchemaBench::INPUT)
    end
  end

  SchemaBench::SCHEMAS.each do |schema_type, schema|
    x.report("#{schema_type}#try") do
      schema.try(SchemaBench::INPUT)
    end
  end

  x.compare!
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
dry-types-0.12.3 benchmarks/hash_schemas.rb
dry-types-0.12.2 benchmarks/hash_schemas.rb
dry-types-0.12.1 benchmarks/hash_schemas.rb
dry-types-0.12.0 benchmarks/hash_schemas.rb
dry-types-0.11.1 benchmarks/hash_schemas.rb
dry-types-0.11.0 benchmarks/hash_schemas.rb
dry-types-0.10.3 benchmarks/hash_schemas.rb
dry-types-0.10.2 benchmarks/hash_schemas.rb
dry-types-0.10.1 benchmarks/hash_schemas.rb
dry-types-0.10.0 benchmarks/hash_schemas.rb
dry-types-0.9.4 benchmarks/hash_schemas.rb
dry-types-0.9.3 benchmarks/hash_schemas.rb
dry-types-0.9.2 benchmarks/hash_schemas.rb
dry-types-0.9.1 benchmarks/hash_schemas.rb
dry-types-0.9.0 benchmarks/hash_schemas.rb