joebobmiles / zustand-middleware-yjs

Zustand middleware that enables sharing of state between clients via Yjs.
MIT License
103 stars 10 forks source link

Experiment with smaller Zustand state updates #52

Closed joebobmiles closed 1 year ago

joebobmiles commented 1 year ago

The middleware asks Zustand to replace the state with a new state object. This feels inefficient, but I need to test to see which is faster: full replacement or using partial updates.

joebobmiles commented 1 year ago

Had a peek at the Zustand source code and learned that, in fact, the larger updates are more efficient. This is due to the replace parameter bypassing an Object.assign() call when set to true. This skips straight to re-assigning the internal state to the new state.

The difference in performance is a constant time operation (reassignment) versus a linear time operation (Object.assign()).