I have a hard time figuring out when to use a getter or a state for best performance.
I will list some scenarios that you are welcome to comment on:
Scenario 1 - Getters VS state in actions and getters
In an action or getter, if you use a list of products multiple times to find a result would you then use getters.products or state.products?
Also, if you needed to use the products 10 times in the same function, would you call getters.products or state.products 10 times or would you assign the products to a variable at the beginning and then use that 10 times? Are there any perfomance gain in any over the others?
Scenario 2 - Getters returning a function
In the Vuex documentation it states that returning a function in a getter will not cache the result of that function. So having a getter for sorting a list of 1000 products would be bad, right? Like:
const getters = {
sortedProducts: state => {
return state.products.sort(a, b => {
...
})
}
}
So when ever a product is updated, which may or may not alter the sorting, then it would do the entire calculation once again, or?
Would it be better to have a state instead that is manually updated by an action and mutation?
Generally, would it ever make sense to have getters returning functions that has to do with large amount of data?