Skip to content

Instantly share code, notes, and snippets.

@TerranceN
Created April 29, 2022 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TerranceN/cda7cdf8b154bf44a199e341140fb219 to your computer and use it in GitHub Desktop.
Save TerranceN/cda7cdf8b154bf44a199e341140fb219 to your computer and use it in GitHub Desktop.
import fs from 'fs';
import * as yjs from "yjs";
import process from 'process';
const comparisons = [
{
update1: fs.readFileSync('/home/terrance/Downloads/1651186108083 hotter-otter e8b59b6ae4', null),
update2: fs.readFileSync('/home/terrance/Downloads/1651186088891 hotter-otter b94ca284fa', null),
},
{
update1: fs.readFileSync('/home/terrance/Downloads/1651186951260 hotter-otter 8b576f85d3', null),
update2: fs.readFileSync('/home/terrance/Downloads/1651186765027 hotter-otter 07269a0010', null),
}
];
const typedKeys = <T extends {}>(x: T): (keyof T)[] => Object.keys(x) as any;
const logMemoryDiff = (before: NodeJS.MemoryUsage, after: NodeJS.MemoryUsage) => {
console.log(typedKeys(after).reduce((acc, k) => ({
...acc,
[k]: Math.round(100 * (after[k] - before[k]) / 1024 / 1024) / 100,
}), {} as {[key: string]: number}));
}
const mergeUpdatesTest = async () => {
for (let updatePair of comparisons) {
const { update1, update2 } = updatePair;
console.log('');
[update1, update2].forEach(update => {
const { structs } = yjs.decodeUpdate(update);
console.log(`${structs.length} structs`);
});
console.log('----------------');
// For some reason one gc() doesn't clean up 'structs' above, but two does
global!.gc!();
global!.gc!();
const memoryBefore = process.memoryUsage();
const beforeTime = Date.now();
const resultingUpdate = yjs.mergeUpdates([update1, update2]);
const afterTime = Date.now();
const memoryAfter = process.memoryUsage();
console.log(`${afterTime-beforeTime} milliseconds`)
console.log('update1.byteLength', update1.byteLength);
console.log('update2.byteLength', update2.byteLength);
console.log('resultingUpdate.byteLength', resultingUpdate.byteLength);
logMemoryDiff(memoryBefore, memoryAfter);
}
};
mergeUpdatesTest();
/*
626707 structs
609449 structs
----------------
613 milliseconds
update1.byteLength 11359087
update2.byteLength 11076124
resultingUpdate.byteLength 22435192
{
rss: 84.63,
heapTotal: 0,
heapUsed: 0.61,
external: 129.3,
arrayBuffers: 129.3
}
1316911 structs
1155241 structs
----------------
1304 milliseconds
update1.byteLength 23811663
update2.byteLength 21135588
resultingUpdate.byteLength 44947233
{
rss: 124.18,
heapTotal: 0.25,
heapUsed: 0.61,
external: 253.21,
arrayBuffers: 253.21
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment