Measuring the size of a YDoc

How might I check the size of a YDoc, including all of its history?

I see in the benchmark code that there is a listener on updateV2 that adds the update’s byteLength to get a sum. Is this the only way? I’d like to know “after the fact” (e.g. something like doc1.totalByteLength() would be ideal) rather than during updates, if possible.

In the benchmark code I measure each update individually, and the size of the complete document docSize. You can listen to single updates and then get their byte-length. Or you can transform the complete Yjs document to an update and then read the size. It is impossible to know beforehand how large a document is. Although the document might have 10k updates, the size of the document could be 100 bytes because of compressed encoding.

You can, however, estimate the size of a document based on the content (for text documents, Yjs incurs about 53% overhead), or based on the stateVector of the document. But in your case - a 3d application - the only reliable way to measure the size of the document is with Y.encodeStateAsUpdate(doc).byteLength

1 Like