/b.rb
Last active
December 24, 2015 17:02
This file contains hidden or 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 'compel' | |
require 'dry-validation' | |
require 'dry/validation/schema/form' | |
params= { | |
'first_name' => 'Joaquim', | |
'birth_date' => '1989-0', | |
'address' => { | |
'line_one' => 'Lisboa', | |
'line_two' => 'Madrit', | |
'post_code' => '1100', | |
'country' => 'PT' | |
} | |
} | |
schema = Class.new(Dry::Validation::Schema::Form) do | |
key(:first_name, &:filled?) | |
key(:last_name, &:filled?) | |
optional(:birth_date) { |v| v.date_time? } | |
key(:address) do |address| | |
address.hash? do | |
address.key(:line_one, &:filled?) | |
address.optional(:line_two, &:filled?) | |
address.key(:post_code) { |v| v.filled? & v.format?(/^\d{4}-\d{3}$/) } | |
address.key(:country_code) { |v| v.inclusion?(%w(PT GB)) } | |
end | |
end | |
end.new | |
Benchmark.ips do |x| | |
x.report('compel') do | |
Compel.run(params) do | |
param :first_name, String, required: true | |
param :last_name, String, required: true | |
param :birth_date, DateTime | |
param :address, Hash do | |
param :line_one, String, required: true | |
param :line_two, String | |
param :post_code, String, required: true, format: /^\d{4}-\d{3}$/ | |
param :country_code, String, in: ['PT', 'GB'] | |
end | |
end | |
end | |
x.report('dry-v') do | |
schema.(params).messages | |
end | |
x.compare! | |
end |
This file contains hidden or 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
Calculating ------------------------------------- | |
compel 311.000 i/100ms | |
dry-v 355.000 i/100ms | |
------------------------------------------------- | |
compel 3.222k (± 4.4%) i/s - 16.172k | |
dry-v 3.591k (± 5.8%) i/s - 18.105k | |
Comparison: | |
dry-v: 3590.8 i/s | |
compel: 3222.4 i/s - 1.11x slower |
Good call. I missed the fact you're using a symbolized keys-hash and Schema::Form
is designed to work with stringified hashes. I updated the gist and indeed the difference in performance is minimal.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi solnic!
Using
Dry::Validation::Schema::Form
it does not validate properly. This is the output:Using
Dry::Validation::Schema
it does validates as I expected and the benchmark is more encouraging for me ;)