Skip to content

Instantly share code, notes, and snippets.

@solnic
Last active December 24, 2015 17:02
Show Gist options
  • Save solnic/764be9ec96a559f5b20d to your computer and use it in GitHub Desktop.
Save solnic/764be9ec96a559f5b20d to your computer and use it in GitHub Desktop.
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
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
@joaquimadraz
Copy link

Hi solnic!

Using Dry::Validation::Schema::Form it does not validate properly. This is the output:

{ :first_name=>[["first_name is missing"], nil], :last_name=>[["last_name is missing"], nil], :address=>[["address is missing"], nil]}

Using Dry::Validation::Schema it does validates as I expected and the benchmark is more encouraging for me ;)

Calculating -------------------------------------
              compel   332.000  i/100ms
               dry-v   480.000  i/100ms
-------------------------------------------------
              compel      3.429k (± 3.5%) i/s -     17.264k
               dry-v      4.970k (± 3.6%) i/s -     24.960k

Comparison:
               dry-v:     4970.1 i/s
              compel:     3428.7 i/s - 1.45x slower

@solnic
Copy link
Author

solnic commented Dec 24, 2015

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