CPAN is a software modules repository famous for the sheer number of solutions it offers free of charge. Not all of them do different things though, and in some cases its pushing TIMTOWTDI to its limits. Sometimes choosing the right module for the job is easy, but when its not you have to compare them in one way or another.
While speed of execution may not be the most important factor in choosing a solution, it can speak for the code quality and feature design. Two programs written in the same language doing exactly the same thing should have quite comparable run times, but feature creep, code bloat or inefficient algorithms could increase that time beyond acceptable level. Some modules can also implement interesting optimization strategies, while others may not or could not due to different design goals.
In this article, I am going to present benchmark results ran on my machine. You should probably try it out yourself on yours too! Source code for these benchmarks can be viewed at https://github.com/bbrtj/perl-validator-benchmark.
Benchmark setup
We're going to see how the following libraries fare against each other in validating a hash reference:
- Data::MuForm
A form framework that was recommended to me on #perl IRC. Even though I haven't used it in any serious project, it seems pretty comprehensive and is capable of more than just validating data (like rendering). It has some maintenance issues, but has seen some activity lately.
- Data::Sah
A very interesting module that lets you compile your rules into a single expression in Perl, JavaScript or human language. It implements a schema language for validating structures called Sah.
- Form::Toolkit
Moose-based, role-heavy framework that can be extended by creating more Moose classes and roles. It focuses on validating the data and doesn't care from where it came from. Was not updated in years, but still passes all tests and noone has reported any issues.
- Form::Tiny
Lightweight data validator inspired by Laravel validation system, Form::Toolkit and Type::Tiny. It does not contain any field validation code, and instead depends on Type::Tiny constraints to deliver them.
- HTML::FormHandler
Very similar to Data::MuForm, but seems to have more rendering and other non-validation capabilities. The most ++'ed module of them all.
- JSON::Schema::Modern
A comprehensive perl implementation of a validator using json schema.
- JSON::Schema::Tiny
A slimmed down version of JSON::Schema::Modern from the same author.
- Type::Tiny
Pure type-based check. Other validators may already use Type::Tiny, but here we just construct a pure Type::Tiny nested structure and validate with that. Yes, that's possible.
- Valiant
Recent addition to CPAN, presented at the last conference. Inspired by Ruby on Rails and meant to be used together with Moo. Marked as early release in the documentation.
- Validate::Tiny
Possibly the smallest validation library on CPAN. May be basic, but thanks to that you have full control over what's going on, and hopefully better performance.
- Validator::LIVR
Perl implementation of Language Independent Validation Rules.
- Whelk
Not actually a full validation framework, but rather lightweight engine with JSON Schema-like validation for use in the Whelk API framework.
System info
Following benchmarks were run on Thinkpad T480 (Intel i7-8650U) running FreeBSD 14.1-RELEASE. Each case for each framework was run for 10 secods.
Perl 5.40.0 (built by perlbrew, not threaded) was used with latest available CPAN dependencies installed by Carmel. Some optional XS modules like Type::Tiny::XS were installed prior to benchmarking.
Case #1: a single field
This will be the most basic hash reference with just a single value:
{
a => 2
}
We don't check for the value here, we just want 'a'
existence in $data
to be ensured.
Results
Rate Speedup vs previous
HtmlFormHandler 929/s --
JsonSchemaModern 964/s 3%
DataMuForm 5282/s 447%
JsonSchemaTiny 15017/s 184%
FormToolkit 30981/s 106%
ValidatorLivr 33017/s 6%
Valiant 39173/s 18%
FormTiny 56271/s 43%
TypeTiny 92754/s 64%
ValidateTiny 128668/s 38%
Whelk 231831/s 80%
DataSah 503022/s 116%
Case #2: multiple fields
A little more complex case, which involves five fields which are all required and string:
{
a => 'test1',
b => 'test2',
c => 'test3',
d => 'test4',
e => 'test5',
}
Results of this case can be used to determine how efficiently each framework is traversing a flat structure.
Results
Rate Speedup vs previous
JsonSchemaModern 320/s --
HtmlFormHandler 456/s 42%
DataMuForm 1958/s 329%
JsonSchemaTiny 2235/s 14%
ValidatorLivr 11139/s 398%
FormToolkit 15050/s 35%
Valiant 16735/s 11%
ValidateTiny 28977/s 73%
FormTiny 29201/s 0%
TypeTiny 34515/s 18%
Whelk 95119/s 175%
DataSah 156334/s 64%
Case #3: array of nested hashes
The last case is an array of 100 hashes:
{
a => [{
b => 5,
c => 'text',
}, {
b => -1,
c => 'another text',
}, {
b => 1000,
c => 'and another',
}, # and 97 hashes more
]
}
This should not only test the framework's ability to validate such structure, but also whether its performance goes down linearly with data amount, or exponentially.
Results
Rate Speedup vs previous
DataMuForm 2.27/s --
HtmlFormHandler 7.87/s 246%
JsonSchemaModern 32.7/s 315%
JsonSchemaTiny 45.1/s 37%
Valiant 112/s 148%
FormToolkit 288/s 157%
ValidateTiny 598/s 107%
FormTiny 796/s 33%
Whelk 1516/s 90%
ValidatorLivr 1829/s 20%
DataSah 2380/s 30%
TypeTiny 6970/s 192%
Conclussion
Left as an exercise for the reader. Now that the numbers are out, you can do with them as you please.
Changelog
- Thu Aug 24 2023: Updated the frameworks and redone the benchmarks, added benchmark machine specs
- Wed Mar 5 2025: Added Whelk, updated the frameworks and redone the benchmarks
This article has been rewritten. The old version can be viewed here
Comments? Suggestions? Send to bbrtj.pro@gmail.com
Published on 2021-10-19