Docs
Tutorial
Encoding

Encoding

Inspired by Automerge, We use a custom binary format to effectively minimize the inherent redundancy in CRDT documents.

As collaboration and synchronization applications have diverse synchronization requirements depending on the scenario, factors such as document size and encoding/decoding time may differ. Loro addresses these factors by offering two unique encoding schemes.

  • Snapshot encoding: encodes the entire document with both the state and all the ops
  • Updates encoding: only encodes the chosen ops, leading to faster encoding but slower decoding.

Snapshot Encoding

const doc1 = new Loro();
const doc2 = new Loro();
 
doc2.getText("text").insert(0, "hello");
doc1.import(doc2.exportSnapshot());
console.log(doc1.toJson()); // { text: "hello" }

The Snapshot encoding scheme encodes the complete State of a document into a snapshot that forms a corresponding version of Loro. Therefore, this scheme is suitable for scenarios where a brand new document requests the latest version or a historical version of the complete content. In scenarios with a large number of historical changes, Loro will encode the State of Loro in a way that is close to the memory layout, so that Loro does not need to rebuild the State from scratch, making the encoding and decoding process super fast. The content of document can be displayed to the user in the shortest possible time.

Use cases

  • Local storage of the document
  • Request the full content of a specified version

Updates Encoding

const doc1 = new Loro();
const doc2 = new Loro();
 
doc2.getText("text").insert(0, "hello");
doc1.import(doc2.exportFrom());
console.log(doc1.toJson()); // { text: "hello" }

The Updates encoding scheme belongs to the incremental update encoding, where the user inputs the starting version and Loro returns the entire State of the document from the starting version to the latest version. As a result, this scheme is suitable for real-time or asynchronous communication requirements in multi-party collaboration scenarios.

Use cases

  • Real-time collaboration
  • Syncing with other peers