joebobmiles / zustand-middleware-yjs

Zustand middleware that enables sharing of state between clients via Yjs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Experiment with smaller Zustand state updates

joebobmiles opened this issue · comments

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.

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()).