-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Poor performance when (over)using setFieldValue/setFieldTouched #4382
Comments
Thanks for reporting this. I was concerned about using arrays due to the perf issue you mentioned here. But the problem with field paths is that they can change, a field name/path can change. So object/Map keys will need to be kept in sync with the fields' current names. Actually <= 4.9 did use that technique of keeping an object for field names and switching to that version reduces the lag considerably, might be a workaround if it is a deal-breaker for you till I figure out how to optimize path states. As for your second suggestion, that is possible to do I believe. It will need to be done at all entry levels for field paths, but it is doable. |
Found a reasonable workaround, we generate a computed property that has the field paths mapped in a map/dict. This guarantees the keys are always up to date and shouldn't perform array finds as often. Also reduces the normalization path calls, thanks for reporting this again and for the great investigation. |
Did another thing, instead of generating the path map/lookup every time, I debounce it by the next tick while at the same time doing an O(1) operation to ensure the map is updated, this means it only generates the lookup once for all 600 fields in your example, it basically waits for the fields to stabilize before generating it, with the earlier commit it ran it 600 times which isn't ideal and wasteful. |
Well... That was quick! Thanks 😁 Out of curiosity: in what scenarios field name/path can change? I could think of only one - when it's in an array and it (or it's ancestor) is moved to lower or higher index by remove/insert of another element or move/swap etc. Are there other scenarios that can affect field name/path? |
You are correct, it is mostly in loops or conditional rendering (if the components don't have a |
What happened?
I have a large form (over 500 inputs), where each input can be edited on its own, but there's also a possibility to "batch" edit multiple fields. When using this batch edit mode I'm calling
setFieldValue
for each field. Also, when using batch edit I'd like to "propagate" touched status to each affected field.It all works fine, except that there's a noticeable lag. While I'm aware that I could use
v-model.lazy
or similar technique to minimize the number ofsetFieldValue
calls I think it would be cool to also improve the perf in vee-validate and make it go brrrr 🚀I created a perf profile during editing and bluring batch inputs and it shows that the offending code is
![image](https://private-user-images.githubusercontent.com/5730169/254999952-43a60bc6-faf1-4307-a313-70a2dafe8d88.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA5OTA3MzYsIm5iZiI6MTcyMDk5MDQzNiwicGF0aCI6Ii81NzMwMTY5LzI1NDk5OTk1Mi00M2E2MGJjNi1mYWYxLTQzMDctYTMxMy03MGEyZGFmZThkODgucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDcxNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA3MTRUMjA1MzU2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YzQzMGZhYWRhNWM0ZWNmMDhiMTQyZjhmOGE3YmUyOGJhMTQ1NjQ4NTM1YmRkZTQyNmE2ZDEwZmE5ODg1MjIwMiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.6g98Jf3AWEkh_Hebq7JbNRtOZVepNY6sIKIQKNUe0mM)
findPathState
and the helper functions that it calls:I think it could be optimized. Right now it calls
pathStates.value.find
which runsnormalizeFormPath
for both operands on each iteration which is wasteful because the path that we're looking for could be normalized once, and the paths on already existingPathState
s could be already normalized. I have couple ideas how it could be improved:findPathState
would need to be normalized and then used to lookup the dictionary/MapPathState
s in a dict/Map we could leave it in an array but makePathState.path
be already normalized path (alternatively, we could store normalized one in a new field, for examplePathState.normalizedPath
) and thennormalizeFormPath
I'm willing to submit a PR implementing one of these if you'd like. Just let me know which direction you think is best.
Reproduction steps
Version
Vue.js 3.x and vee-validate 4.x
What browsers are you seeing the problem on?
Relevant log output
No response
Demo link
https://stackblitz.com/edit/vee-validate-v4-checkboxes-twbzx2?file=src%2FApp.vue
Code of Conduct
The text was updated successfully, but these errors were encountered: