-
-
Save eugene-eeo/cbe85d83e9a0bf3c5e7ddda3481803c0 to your computer and use it in GitHub Desktop.
automerge wasm hmm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const process = require('process') | |
// use either 'normal' or WASM automerge here | |
const Automerge = require('./automerge/src/automerge') | |
// const Automerge = require('./bench/automerge-pinned') | |
function local_1(doc, edit) { | |
const new_doc = Automerge.change(doc, d => { | |
if (edit[1] !== 0) { | |
d.text.deleteAt(edit[0], edit[1]) | |
} else { | |
d.text.insertAt(edit[0], ...edit[2]) | |
} | |
}) | |
return [new_doc, Automerge.getChanges(doc, new_doc)] | |
} | |
function local_2(doc, edit) { | |
const [new_doc, change] = Automerge.Frontend.change(doc, d => { | |
if (edit[1] !== 0) { | |
d.text.deleteAt(edit[0], edit[1]) | |
} else { | |
d.text.insertAt(edit[0], ...edit[2]) | |
} | |
}) | |
return [new_doc, [Automerge.encodeChange(change)]] | |
} | |
function microLTR(length) { | |
const edits = [] | |
for (let i = 0; i < length; i++) | |
edits.push([i, 0, 'a']) | |
return edits | |
} | |
// Change here | |
const REPEATS = 5 | |
const LENGTH = 1000 | |
const METHOD = local_1 // which method to use? | |
const DATA = microLTR(LENGTH) | |
for (let n = 1; n <= REPEATS; n++) { | |
let t0, t1 | |
let change | |
let doc1 = Automerge.init(); | |
[doc1, change] = Automerge.Frontend.change(doc1, d => d.text = new Automerge.Text()) | |
let doc2 = Automerge.init() | |
doc2 = Automerge.applyChanges(doc2, [Automerge.encodeChange(change)]) | |
let log = [] | |
t0 = process.hrtime.bigint() | |
// Local | |
for (let edit of DATA) { | |
[doc1, change] = METHOD(doc1, edit) | |
log.push(change) | |
} | |
t1 = process.hrtime.bigint() | |
console.log(`local: ${(Number(t1 - t0) / 1000000).toFixed(2)} ms`) | |
// Remote | |
t0 = process.hrtime.bigint() | |
for (let change of log) { | |
doc2 = Automerge.applyChanges(doc2, change) | |
} | |
t1 = process.hrtime.bigint() | |
console.log(`remote: ${(Number(t1 - t0) / 1000000).toFixed(2)} ms`) | |
Automerge.free(doc1) | |
Automerge.free(doc2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment