Skip to content

Instantly share code, notes, and snippets.

@EvanHahn
Created June 17, 2024 19:40
Show Gist options
  • Save EvanHahn/7f915274ece9e34997bf2497d0aac1bf to your computer and use it in GitHub Desktop.
Save EvanHahn/7f915274ece9e34997bf2497d0aac1bf to your computer and use it in GitHub Desktop.
// @ts-check
import { generate } from '@mapeo/mock-data'
import { encode } from '@mapeo/schema'
import Hypercore from 'hypercore'
import RAM from 'random-access-memory'
import { gzipSync } from 'zlib'
/**
* @param {ReadonlyArray<any>} records
* @returns {Promise<number>}
*/
const getUncompressedSize = async (records) => {
const ram = new RAM()
const hypercore = new Hypercore(() => ram)
await hypercore.ready()
await Promise.all(records.map((record) => hypercore.append(encode(record))))
return ram.toBuffer().byteLength
}
/**
* @param {ReadonlyArray<any>} records
* @returns {Promise<number>}
*/
const getCompressedSize = async (records) => {
const ram = new RAM()
const hypercore = new Hypercore(() => ram)
await hypercore.ready()
await Promise.all(records.map((record) => hypercore.append(encode(record))))
const uncompressedBuffer = ram.toBuffer()
return gzipSync(uncompressedBuffer, { level: 9 }).byteLength
}
const main = async () => {
const records = [
...generate('projectSettings', { count: 100 }),
...generate('observation', { count: 10_000 }),
...generate('field', { count: 1000 }),
...generate('preset', { count: 1000 }),
...generate('role', { count: 100 }),
...generate('deviceInfo', { count: 100 }),
// `translation` and `track` are missing from `@mapeo/mock-data`
// `icon` and `coreOwnership` had issues I did not investigate
]
console.log('Record count:', records.length)
const uncompressedSize = await getUncompressedSize(records)
console.log('Uncompressed size:', uncompressedSize)
const compressedSize = await getCompressedSize(records)
console.log('Compressed size:', compressedSize)
}
main().catch((err) => {
console.error(err)
process.exit(1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment