Skip to content

Comparison ​

Features ​

AssertRespect\ValidationLaravel validationwebmozart/assert
Rules are objects you build once and reuseyesyesarrays of rule stringsno, functions
One field compared with anotherany rule, any operandthrough a callbacksome rules (gte:field, same:field, ...)no
Negation of any rulenot(), composites by De Morgannot()nono
All failures at onceyesassert()yesno, stops at the first
Failure as datacode, path, parametersa tree of exceptionsmessages by fieldan exception message
PHP8.3+8.5+ for 3.x8.3+ for 13.x

Speed ​

The benchmark validates an order with N lines, each checked for a SKU pattern and a quantity between 1 and a limit taken from the order. PHP 8.3.6, illuminate/validation 13.45, respect/validation 2.5 (3.x needs PHP 8.5). Time is the median of one validation.

Valid order ​

LinesAssertLaravelRespect
100.19 ms2.95 ms0.11 ms
1001.77 ms22 ms0.71 ms
1 00017 ms219 ms6.5 ms
10 000170 ms2 341 ms72 ms

Every line invalid, all errors collected ​

LinesAssertLaravelRespect
1002.3 ms29 ms8.8 ms, 4.4 MB
1 00022 ms, 0.9 MB278 ms, 2.6 MB98 ms, 44 MB
10 000252 ms, 8.9 MB2 929 ms, 27 MB1 106 ms, 441 MB
  • Against Laravel, Assert is about 13× faster on valid data and 12× faster on invalid data.
  • Against Respect, Assert is about 2.5× slower on valid data: each of its rules resolves its operands, because any of them can be a reference into the data. On invalid data it is about 4.5× faster and uses about 50× less memory, because Respect builds a tree of exceptions for its messages.

The numbers depend on the machine; the script is in the repository, so you can run it on yours.

When to pick something else ​

  • You only guard function arguments and want PHPStan or Psalm to narrow types after the check: webmozart/assert.
  • You are inside Laravel forms and want its error bags, translations and old() input: Laravel validation.
  • You need one of 150+ ready rules (phone numbers, credit cards, ISBN) and validate mostly valid data in hot loops: Respect\Validation.