yjs / y-indexeddb

IndexedDB database adapter for Yjs
https://docs.yjs.dev/ecosystem/database-provider/y-indexeddb
Other
196 stars 30 forks source link

Performance on `IndexeddbPersistence` #29

Open himself65 opened 1 year ago

himself65 commented 1 year ago

I'm investigating the benchmark between y-indexeddb and our @toverything/y-indexeddb (not published yet) because of Our application's requirements for the first screen speed.

I run some basic bench(not accurate), but it looks like the boost speed when creating multiple persistence is much slower than the package we maintained

create many persistence
[
  PerformanceMeasure {
    name: 'yjs',
    entryType: 'measure',
    startTime: 1138.3797089457512,
    duration: 3805.7039580345154,
    detail: null
  },
  PerformanceMeasure {
    name: 'toeverything',
    entryType: 'measure',
    startTime: 4944.275416970253,
    duration: 1129.9851250052452,
    detail: null
  }
]
async function yjs_create_persistence(n = 1e3) {
  for (let i = 0; i < n; i++) {
    const yDoc = new Y.Doc();
    const persistence = new IndexeddbPersistence('test', yDoc);
    await persistence.whenSynced;
    persistence.destroy();
  }
}
async function toeverything_create_provider(n = 1e3) {
  for (let i = 0; i < n; i++) {
    const yDoc = new Y.Doc();
    const provider = createIndexedDBProvider('test', yDoc);
    provider.connect();
    await provider.whenSynced;
    provider.disconnect();
  }
}

Source Code

dmonad commented 1 year ago

Hi @Himself65,

y-indexeddb performs some (non-blocking!) sanity checks at startup. However, the whenSynced event fires after the sanity checks are performed. I just added a change so that whenSynced is fired as soon as all updates are retrieved. Now the whenSynced event fires in 5ms instead of 40ms.

Note that instantiating the y-indexeddb database never blocks the main-thread. However, it requires database access. Please be careful when creating multiple y-indexeddb instances in series. Usually, we only need one. We can only destroy/create a new y-indexeddb provider once all sanity checks are performed.