CPAN form validators benchmark

Raw validation speed - which one is the fastest?


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.

System info

Following benchmarks were run on Thinkpad T480 (Intel i7-8650U) running FreeBSD 13.2-RELEASE-p2. Each case for each framework was run for 10 secods.

Perl 5.38.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     921/s                  --
JsonSchemaModern   1467/s                 59%
DataMuForm         4948/s                237%
JsonSchemaTiny    14637/s                195%
FormToolkit       29301/s                100%
ValidatorLivr     34270/s                 16%
Valiant           41622/s                 21%
FormTiny          52455/s                 26%
TypeTiny          91489/s                 74%
ValidateTiny     131269/s                 43%
DataSah          468761/s                257%

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
HtmlFormHandler     376/s                  --
JsonSchemaModern    601/s                 59%
JsonSchemaTiny     1810/s                201%
DataMuForm         1883/s                  4%
ValidatorLivr      8771/s                365%
FormToolkit       12353/s                 40%
Valiant           13755/s                 11%
ValidateTiny      20947/s                 52%
FormTiny          24783/s                 18%
TypeTiny          26880/s                  8%
DataSah          125150/s                365%

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       1.96/s                  --
HtmlFormHandler  6.30/s                221%
JsonSchemaModern 31.1/s                393%
JsonSchemaTiny   37.5/s                 20%
Valiant          95.6/s                154%
FormToolkit       232/s                142%
ValidateTiny      443/s                 90%
FormTiny          680/s                 53%
ValidatorLivr    1317/s                 93%
DataSah          1999/s                 51%
TypeTiny         4785/s                139%

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

This article has been rewritten. The old version can be viewed here


Comments? Suggestions? Send to feedback@bbrtj.eu
Published on 2021-10-19