Back to articles
Laravel's wildcard validation is O(n ) — here's how to fix it
How-ToSystems

Laravel's wildcard validation is O(n ) — here's how to fix it

via Dev.toSander Muller

I maintain several large Laravel applications. Last year I was profiling an import endpoint that accepts JSON payloads with up to a hundred items. Each item has around 47 fields, many with conditional rules like exclude_unless and required_if . The endpoint was taking 3.4 seconds. I assumed it was database queries. It wasn't. Validation alone was taking 3.2 of those 3.4 seconds. The database work was 200ms. I spent a good part of a day on finding out why. Once I figured out what was causing it, I submitted 10 performance PRs to Laravel. Most were closed. As Taylor always says: "Consider releasing it as a package". So I did, and it brings that 3.2s down to 83ms of time spent on validation. What actually happens when you validate arrays When you write this: 'items.*.name' => 'required|string|max:255' , 'items.*.qty' => 'required|numeric|min:1' , Laravel's explodeWildcardRules() expands the wildcards into concrete rules for every item in the data: items.0.name => required|string|max:255 i

Continue reading on Dev.to

Opens in a new tab

Read Full Article
2 views

Related Articles