Zustand persist (JSON.stringify) performance #2125
Unanswered
MartinMouritzen
asked this question in
General
Replies: 1 comment 6 replies
-
@MartinMouritzen what about using |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First of all, thanks for the best stage management tool. ❤️
My team is using Zustand for a whiteboard app, and on some boards we have A LOT of nested elements. Zustand mostly handles this fine.
The problem comes when we modify state often. An example could be dragging with the mouse to select and having a "selected" property on each element on the board - this could change many times in a second, and the app clearly starts lagging here. I have debugged it to be the persistence in Zustand. We use
idb-keyval
and at first I thought to debounce setItem - but that didn't solve anything. Then I tried to create another middleware before that, but the problem was the same. I have now pinpointed it to be the JSON.stringify that happens before the persist middleware is even called.Now my question: What would be the best way to solve this?
The only solution we can really think of right now would be to not use the persist middleware (which completely removes the performance bottleneck), but then saving state every 1-2 seconds if there's any changes, which we deem would probably be an okay user experience. But we are VERY open to other and better ways to solve this.
Beta Was this translation helpful? Give feedback.
All reactions