Sha256: 1f1e6a413c56ec23d3917d2326c4e808a2314f8683d4f0e15e7f2e12f1e54d22

Contents?: true

Size: 1.24 KB

Versions: 9

Compression:

Stored size: 1.24 KB

Contents

Bundler.require(:benchmarks)

require "dry-initializer"
class DryTest
  extend Dry::Initializer[undefined: false]

  option :foo, proc(&:to_s)
  option :bar, proc(&:to_s)
end

class DryTestUndefined
  extend Dry::Initializer

  option :foo, proc(&:to_s)
  option :bar, proc(&:to_s)
end

class PlainRubyTest
  attr_reader :foo, :bar

  def initialize(options)
    @foo = options[:foo].to_s
    @bar = options[:bar].to_s
  end
end

require "virtus"
class VirtusTest
  include Virtus.model

  attribute :foo, String
  attribute :bar, String
end

require "fast_attributes"
class FastAttributesTest
  extend FastAttributes

  define_attributes initialize: true do
    attribute :foo, String
    attribute :bar, String
  end
end

puts "Benchmark for instantiation with coercion"

Benchmark.ips do |x|
  x.config time: 15, warmup: 10

  x.report("plain Ruby") do
    PlainRubyTest.new foo: "FOO", bar: "BAR"
  end

  x.report("dry-initializer") do
    DryTest.new foo: "FOO", bar: "BAR"
  end

  x.report("dry-initializer (with UNDEFINED)") do
    DryTestUndefined.new foo: "FOO", bar: "BAR"
  end

  x.report("virtus") do
    VirtusTest.new foo: "FOO", bar: "BAR"
  end

  x.report("fast_attributes") do
    FastAttributesTest.new foo: "FOO", bar: "BAR"
  end

  x.compare!
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dry-initializer-3.0.2 benchmarks/with_coercion.rb
dry-initializer-3.0.1 benchmarks/with_coercion.rb
dry-initializer-3.0.0 benchmarks/with_coercion.rb
dry-initializer-2.5.0 benchmarks/with_coercion.rb
dry-initializer-2.4.0 benchmarks/with_coercion.rb
dry-initializer-2.3.0 benchmarks/with_coercion.rb
dry-initializer-2.2.0 benchmarks/with_coercion.rb
dry-initializer-2.1.0 benchmarks/with_coercion.rb
dry-initializer-2.0.0 benchmarks/with_coercion.rb