Question: Performance Optimization in Zustand #2974
-
Question: Performance Optimization in ZustandHello! 👋 I'm using Zustand and trying to maximize performance. I’m unsure which approach is better when selecting slices of the store. Specifically, should I return slices independently, or use Option 1: Selecting Independentlyconst nuts = useBearStore((state) => state.nuts);
const honey = useBearStore((state) => state.honey); Option 2: Using useShallow to Combineconst { nuts, honey } = useBearStore(
useShallow((state) => ({ nuts: state.nuts, honey: state.honey }))
); Contextnuts: An array of objects. Which option is more performant and why? Are there scenarios where one approach is clearly better than the other? Any insights would be appreciated! Extra: Does this compare to the performance of selection only one slice? Thanks in advance! 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
@dev2xl option 1 > option 2, option 1 you don't need to import anything and does not need to use a shallow comparison Note that's true only for primitive data types for complex data types you might need |
Beta Was this translation helpful? Give feedback.
@dev2xl option 1 > option 2, option 1 you don't need to import anything and does not need to use a shallow comparison
Note
that's true only for primitive data types for complex data types you might need
useShallow
, let's say you are comparingMap
s orSet
s then you need to useuseShallow