The huge form is too slow
I have a form that has 200+ fields.
When saving I get the timeout error
Maximum execution time of 30 seconds exceeded
Any ideas on how to speed this up?
The form is generated automatically (not static).
16 Replies
but saving the form without data happens pretty quickly
I'd remove some stuff to see which parts take long to save
Impossible to debug a 200 field form otherwise
but even when I get a timeout, the form still doesn't save because I delete all the fields. so the problem can't be in the database queries.
it looks like it's because of the fields validation. it looks like for each field the entire form is built from scratch (you can see it on the video).
Maybe some
Select
fields that load a lot of datathere are no select fields. only text, checkbox and radio. I will try to delete parts to find out what the problem is
it looks like for each field the entire form is built from scratch (you can see it on the video).I don't think it's for every field, but it's definitely called multiple times. You shouldn't have logn operations in the form definition
I found the problem. It's because I pass the function in which the tabs are built.
When I removed the function, the saving speed became instant.
But now the problem is that I need to somehow get the
$record
variable 😤it seems that the problem is not in the closure, but in
FieldData::from
(Spatie\LaravelData). when I remove it, everything works fast.
but why then does it work quickly without closure which I showed in the video.. im completely confused 🫤What is the DTO doing under the hood?
standard DTO, nothing custom 🤷♂️
Very weird. I can’t imagine why that would be the bottleneck.
from
method slows down codeDon’t know enough about the spatie package, sorry. I can’t imagine what it’s doing to the field. Are the fields in the map just an array or are they instances of a class.
yes these are instances (eloquent). I couldn't figure out what the problem was.
The solution is this: I created a custom page for the resource and added all the fields there. This way I was able to do it without using closure. now it works fast there.
@Noxo Which version of spatie/laravel-data do you use?
::from
usually triggers validation for all fields, which might slow things down considerably. Regular new
instantiation should skip validation and be orders of magnitude faster as you observed. If you use v4, you may want to try out the Dto
class: https://spatie.be/docs/laravel-data/v4/as-a-data-transfer-object/creating-a-data-object#content-dto-classes
I guess you can skip validation when you are building the form from existing database-driven field definitions, as these have been validated at creation/update.Creating a data object | laravel-data
laravel-data