%O3tQKuu5bD4uLCtRYwg3AQg5FR5v0XYn6kLoeOsgH8o=.sha256
was wondering about compressing msg content (before encrypting it), so had a quick poke with brotli
const log = console.log
const round = num => Math.floor(num * 100) / 100
const brotli = require('brotli')
const content = {
type: 'profile/person',
preferredName: { set: 'mix' },
description: { set: 'a father of two, and programmer of scuttlebutt, ahau. enjoys sci-fi, video games, and planting native trees' },
authors: {
'@ye2312309187102931l238172387123017298=.ed25519': {
350: 1
}
},
tangles: {
profile: { root: null, previous: null },
group: {
root: '%asdajsed123qkajsdlXCSDCIUjazskjd=.sha256',
previous: [
'%3234axaseadDEEascdcawseasdDWSDjd=.sha256'
]
}
},
recps: [
'%asdajsed123qkajsdlXCSDCIUjazskjd=.cloaked'
]
}
/* compress */
const buf = Buffer.from(JSON.stringify(content), 'utf8')
const jsonSize = JSON.stringify(content).length
log('initial size', { json: jsonSize, buf: buf.length }, '\n')
console.time('compressed')
const output = brotli.compress(buf, {
mode: 1, // 0 = generic, 1 = text, 2 = font (WOFF2)
quality: 8, // 0 - 11
lgwin: 22 // window size
})
console.timeEnd('compressed')
const base64Size = output.toString('base64').length
log('compressed size:', { base64: base64Size, buf: output.length })
log('ratio:', round(jsonSize / base64Size), '\n')
/* decompress */
console.time('decompressed')
const arrayBuffer = brotli.decompress(output)
const original = Buffer.from(arrayBuffer)
console.timeEnd('decompressed')
if (!original.equals(buf)) log('error!')
Output:
initial size { json: 484, buf: 484 }
compressed: 122.854ms
compressed size: { base64: 1061, buf: 291 }
ratio: 0.45
decompressed: 1.456ms