Let's use Benchmark::Featureset::ParamCheck to see how fast Type::Params is compared with other modules for validating positional parameters. (Hint: very fast.)
The results of running the script on a fairly low-powered laptop. Each parameter checking implementation is called 250,000 times. The table below displays the average time taken for each call in nanoseconds.
Type::Params .................................... 2640 ns (378787/s) Params::ValidationCompiler with Type::Tiny ...... 3120 ns (320512/s) Pure Perl Implementation with Ref::Util::XS ..... 3639 ns (274725/s) Naive Pure Perl Implementation .................. 4600 ns (217391/s) Params::ValidationCompiler with Specio .......... 11719 ns (85324/s) Params::ValidationCompiler with Moose ........... 12079 ns (82781/s) Data::Validator with Mouse ...................... 51760 ns (19319/s) Data::Validator with Type::Tiny ................. 51920 ns (19260/s) Data::Validator with Moose ...................... 52120 ns (19186/s) MooseX::Params::Validate with Moose ............. 83080 ns (12036/s) MooseX::Params::Validate with Type::Tiny ........ 84839 ns (11786/s)
Pure Perl Implementation with Ref::Util::XS ..... 3560 ns (280898/s) Naive Pure Perl Implementation .................. 4479 ns (223214/s) Type::Params .................................... 7879 ns (126903/s) Params::ValidationCompiler with Type::Tiny ...... 8319 ns (120192/s) Params::ValidationCompiler with Specio .......... 11800 ns (84745/s) Params::ValidationCompiler with Moose ........... 12159 ns (82236/s) Data::Validator with Type::Tiny ................. 51039 ns (19592/s) Data::Validator with Moose ...................... 51559 ns (19394/s) Data::Validator with Mouse ...................... 51760 ns (19319/s) MooseX::Params::Validate with Type::Tiny ........ 82800 ns (12077/s) MooseX::Params::Validate with Moose ............. 93160 ns (10734/s)
Type::Params (using Type::Tiny type constraints) provides the fastest convenient way of checking positional parameters for a function, whether or not Type::Tiny::XS is available.
The only way to beat it is to write your own type checking in longhand, but if Type::Tiny::XS is installed, you probably still won't be able to match Type::Params' speed.
Params::ValidationCompiler (also using Type::Tiny type constraints) is very nearly as fast.
Params::ValidationCompiler using other type constraints is also quite fast, and when Type::Tiny::XS is not available, Moose and Specio constraints run almost as fast as Type::Tiny constraints.
Data::Validator and MooseX::Params::Validate are far slower.
To run this script, you will need:
Module::Runtime, Benchmark::Featureset::ParamCheck.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2013-2014, 2017 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Type::Tiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Type::Tiny
CPAN shell
perl -MCPAN -e shell install Type::Tiny
For more information on module installation, please visit the detailed CPAN module installation guide.