Created
July 20, 2020 07:42
-
-
Save HashNotAdam/08bd05db7083887f59434e7b22013738 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark/ips' | |
require 'active_model' | |
require 'dry-validation' | |
require 'dry/schema/version' | |
GOOD_INPUTS = [ | |
{ name: 'Tom', age: 904 }, | |
{ name: 'Piotr', age: nil }, | |
{ name: 'Dane', age: 44, password: 'wigwam' }, | |
] | |
BAD_INPUTS = [ | |
{ name: nil, age: nil }, # nil not allowed for name | |
{ name: 'Tom', age: 'hello' }, # string not allowed for age | |
{ }, # all keys missing | |
] | |
class ActivePerson | |
include ActiveModel::Model | |
attr_accessor :name, :age, :password | |
validates_presence_of :name | |
validates_numericality_of :age, only_integer: true, allow_nil: true | |
end | |
class DryPerson < Dry::Validation::Contract | |
schema do | |
required(:name).value(:string) | |
required(:age).maybe(:integer) | |
end | |
end | |
def validate_inputs | |
GOOD_INPUTS.each do |input| | |
yield(input) | |
end | |
BAD_INPUTS.each do |input| | |
yield(input) | |
end | |
end | |
def title(str) | |
str.rjust(25) | |
end | |
Benchmark.ips do |x| | |
x.report(title("ActiveModel #{ActiveModel::VERSION::STRING}")) do | |
validate_inputs { |input| ActivePerson.new(input).valid? } | |
end | |
x.report(title("dry-schema, Params, #{Dry::Schema::VERSION}")) do | |
validate_inputs { |input| DryPerson.new.(input) } | |
end | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment