Several small updates: merge updates then apply vs. apply all updates directly

Is it faster to merge the updates before applying to the Doc? Does it make any difference?

Thanks.

If you have many small update actions, it is recommended that you find a balance between the number of updates and the size of updates, which may have a certain impact on network transmission.

For example, when you need to merge multiple small updates, you can use doc.transact(()=>{/*actions are here. */});

If you have the Yjs document already loaded to memory, then it is always best to apply updates directly to a Yjs document (ideally all updates during a single transaction):

ydoc.transact(() => {
  updates.forEach(update => 
    Y.applyUpdate(ydoc, update)
  )
})

If you only have partial updates (e.g. x latest messages cached) and you don’t want to load the Yjs document, then you should use Y.mergeUpdates.

Note that Y.mergeUpdates doesn’t perform garbage-collection. Hence it is always recommended to load the document to a Yjs document from time to time to perform garbage-collection.

1 Like

Thank you, @jarone @dmonad.