Consider mutating the state #1232
Replies: 3 comments 3 replies
-
@CodeDredd I noticed you've pushed a new update and wanted to bring your attention to this idea if you have some spare time. Number 2 is a pretty simple change with massive gains and doesn't seem to have any downsides since vue uses proxies anyway to notify changes |
Beta Was this translation helpful? Give feedback.
-
@CodeDredd @edusperoni now that #1604 has been merged and released as part of 1.7.0, I was wondering.. are you satisfied with the performance now? For me, I'm just now migrating from vuex-orm and the act of saving a large response that essentially contains hundreds (not thousands) of records and nested relationships, still feels much much slower than it was with vuex-orm. I can't even imagine how slow it may have been before this PR was merged. But unfortunately.. it's still unacceptably slow.. so I have quite the dilemma on my hands. Do you feel like it's fast enough for your use-case now? Any insights as to why it might be much slower than it was with vuex-orm in my case? Many thanks |
Beta Was this translation helpful? Give feedback.
-
I created a PR #1773 because this implementation is breaking Vue 2 reactivity |
Beta Was this translation helpful? Give feedback.
-
I'm diving a bit into pinia-orm as we intend on using it for storing a lot of data and need it to be blazing fast (which is why I opened #1206).
Another big issue for us is that store manipulation is painfully slow when you have lots of entities.
I managed to narrow it down to the way the entities are inserted/updated/removed from the store. For example:
While this is fine and usually fast in plain JS, Vue's reactivity make it awfully slow, as it goes through all changed items (in this case, everything, since we changed
data
itself), so take this example:https://stackblitz.com/edit/vue-3-composition-api-demo-ez5csp?file=src%2Fpinia.ts
you'll see that timeToRun will increase progressively. With a big enough size, time to save an item can take over 15ms. I believe the complexity of this code with vue reactivity enabled is O(n²), as each insert operation is O(n) (goes through all items).
In sum, if your store is big enough, every save, insert, delete, etc will take a significant amount of time to run. Deletes are particularly heavy because delete complexity is already O(n²) by iterating over existing items and using
array.includes
.This all, of course, can be mitigated in 2 ways:
data
non-reactive. By using markRaw we make the data object itself non-reactive, reducing the complexity to O(n). I'm not sure about the full implications of this though. From what I gathered, the entities themselves would still be reactive.Object.assign
. Although I'm not a fan of state mutation at all, that seems to be the strength of vue's reactivity. By doing this the complexity goes down massively as vue will only iterate over changed items. The same has to be done todelete
as well (use the delete operator instead of changing the state).I'd be happy to implement these changes in a PR, but wanted to discuss them first as they can change architecture/behavior for (advanced) users
Beta Was this translation helpful? Give feedback.
All reactions