Comparison
Features
| Assert | Respect\Validation | Laravel validation | webmozart/assert | |
|---|---|---|---|---|
| Rules are objects you build once and reuse | yes | yes | arrays of rule strings | no, functions |
| One field compared with another | any rule, any operand | through a callback | some rules (gte:field, same:field, ...) | no |
| Negation of any rule | not(), composites by De Morgan | not() | no | no |
| All failures at once | yes | assert() | yes | no, stops at the first |
| Failure as data | code, path, parameters | a tree of exceptions | messages by field | an exception message |
| PHP | 8.3+ | 8.5+ for 3.x | 8.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
| Lines | Assert | Laravel | Respect |
|---|---|---|---|
| 10 | 0.19 ms | 2.95 ms | 0.11 ms |
| 100 | 1.77 ms | 22 ms | 0.71 ms |
| 1 000 | 17 ms | 219 ms | 6.5 ms |
| 10 000 | 170 ms | 2 341 ms | 72 ms |
Every line invalid, all errors collected
| Lines | Assert | Laravel | Respect |
|---|---|---|---|
| 100 | 2.3 ms | 29 ms | 8.8 ms, 4.4 MB |
| 1 000 | 22 ms, 0.9 MB | 278 ms, 2.6 MB | 98 ms, 44 MB |
| 10 000 | 252 ms, 8.9 MB | 2 929 ms, 27 MB | 1 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.