Sha256: 29a21a083042a1f1f15340ac85ca9bee5137bafebfb8a8129002865079d77eef

Contents?: true

Size: 1.2 KB

Versions: 11

Compression:

Stored size: 1.2 KB

Contents

require 'benchmark/ips'
require 'parametric/struct'

StructAccount = Struct.new(:id, :email, keyword_init: true)
StructFriend = Struct.new(:name, keyword_init: true)
StructUser = Struct.new(:name, :age, :friends, :account, keyword_init: true)

class ParametricAccount
  include Parametric::Struct
  schema do
    field(:id).type(:integer).present
    field(:email).type(:string)
  end
end

class ParametricUser
  include Parametric::Struct
  schema do
    field(:name).type(:string).present
    field(:age).type(:integer).default(42)
    field(:friends).type(:array).schema do
      field(:name).type(:string).present
    end
    field(:account).type(:object).schema ParametricAccount
  end
end

Benchmark.ips do |x|
  x.report("Struct") {
    StructUser.new(
      name: 'Ismael',
      age: 42,
      friends: [
        StructFriend.new(name: 'Joe'),
        StructFriend.new(name: 'Joan'),
      ],
      account: StructAccount.new(id: 123, email: 'my@account.com')
    )
  }
  x.report("Parametric::Struct")  {
    ParametricUser.new!(
      name: 'Ismael',
      age: 42,
      friends: [
        { name: 'Joe' },
        { name: 'Joan' }
      ],
      account: { id: 123, email: 'my@account.com' }
    )
  }
  x.compare!
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
parametric-0.2.21 bench/struct_bench.rb
parametric-0.2.20 bench/struct_bench.rb
parametric-0.2.19 bench/struct_bench.rb
parametric-0.2.18 bench/struct_bench.rb
parametric-0.2.17 bench/struct_bench.rb
parametric-0.2.16 bench/struct_bench.rb
parametric-0.2.15 bench/struct_bench.rb
parametric-0.2.14 bench/struct_bench.rb
parametric-0.2.13 bench/struct_bench.rb
parametric-0.2.12 bench/struct_bench.rb
parametric-0.2.11 bench/struct_bench.rb