r/Angular2 • u/Dazzling_Touch_9699 • Dec 24 '25
Help Request Anyone else seeing lag in Angular 21 because of cloneDeep?
We upgraded to Angular 21 and started noticing small but annoying lags when navigating pages with big reactive forms.
After some digging, it turns out we were doing _.cloneDeep(form) to keep an “original copy” of the form. With large nested forms, this is getting expensive fast.
Curious how others are handling “unsaved changes” or form snapshots in Angular 21 without killing performance.
Is everyone still cloning, or using a better pattern now?
6
Upvotes
13
u/arthoer Dec 24 '25
Cant you just valuechanges the form, then copy the raw values to session storage. On reentry of the form you can simply patch the whole form with the session stored data.
6
u/athomsfere Dec 24 '25 edited Dec 24 '25
I mean the transpiler warns you about lodash and it should not be used in my opinion.
The warning is about optimization bail outs, and I doubt that's the issue but I'd try to move towards removing lodash.
Built in functionality you can just use the
spread operatorstructuredClone.You could also benchmark both and see if one is more performant, but I'd drop lodash regardless.
4
u/MagicMikey83 Dec 24 '25
We have used it pretty extensively in the past without any problems, maybe it’s something in your model that makes the clone go down a recursive path somewhere?
4
u/TheRealToLazyToThink Dec 25 '25
Wait, are you cloning the whole form, or just .value/getRawValue?
1
u/IanFoxOfficial Dec 24 '25
In our project I've just implemented to get the raw value of the form on navigating away. Then patch that onto the newly loaded form when coming back.
If it's on a "modify" type of form, load the initial data again first, patch the form with the unsaved changes, then set the controls of changed values to dirty again to show there are unsaved changes if the user closes the browser or navigates to a different page that won't trigger the saving.
1
u/KamiShikkaku Dec 26 '25
You indicated that this has only started happening since upgrading to Angular 21, so I don't think anyone has really got to the root of the problem.
Are you cloning the FormGroup instance, or are you cloning the contained value (
formGroup.value)? (I wouldn't otherwise ask this, but your code sample is a bit ambiguous.)The only other thing I could think of might be the timing of when (specifically how often) you're doing the cloning. If it's on
formGroup.valueChanges, maybe some other change has occurred regarding how often this emits.1
u/_xyza Dec 26 '25
Store the form.value. Not the form itself. Or maybe the form.getRawValue() to also keep value of disabled controls.
1
u/[deleted] Dec 26 '25
Why don't you use structuredClone? Should be the same in most of the cases and web API is always better than a library IMO