MsgPack
'use strict'
const fs = require('fs')
const path = require('path')
const assert = require('assert')
const { encode, decode } = require('@msgpack/msgpack')
const pkg = require('./package-minimal.json')
const jsonString = JSON.stringify(pkg)
const pkgEncode = encode(pkg)
console.log(`JSON Size: ${Buffer.byteLength(jsonString)}`)
console.log(`MSGPack Size: ${Buffer.byteLength(pkgEncode)}`)
assert(pkg, decode(pkgEncode))
fs.writeFileSync(path.join(__dirname, "out", "package-msgpack.dat"), pkgEncode)
// JSON Size: 105
// MSGPack Size: 79
Protobufjs
'use strict'
const fs = require('fs')
const path = require('path')
const assert = require('assert')
const protobufjs = require('protobufjs')
const pkg = require('./package-minimal.json')
const main = async () => {
const root = protobufjs.load(path.join(__dirname, 'schema', 'package.proto'))
const Package = (await root).lookupType('Package')
const err = Package.verify(pkg)
if (err) throw err
const message = Package.create(pkg)
const buffer = Package.encode(message).finish()
console.log(`JSON Size: ${Buffer.byteLength(JSON.stringify(pkg))}`)
console.log(`Protobuf Size: ${Buffer.byteLength(buffer)}`)
const decodeMsg = Package.decode(buffer)
const obj = Package.toObject(decodeMsg)
assert.deepStrictEqual(pkg, obj)
fs.writeFileSync(path.join(__dirname, "out", "package-protobuf.dat"), buffer)
}
main().catch(err => console.error)
// JSON Size: 105
// Protobuf Size: 44
Avro
'use strict'
const fs = require('fs')
const path = require('path')
const assert = require('assert')
const avro = require('avsc')
const pkg = require('./package-minimal.json')
const schema = fs.readFileSync(path.join(__dirname, 'schema', 'Package.avsc.json'), 'utf8')
const type = avro.Type.forSchema(JSON.parse(schema))
const buffer = type.toBuffer(pkg)
const value = type.fromBuffer(buffer)
console.log(`JSON Size: ${Buffer.byteLength(JSON.stringify(pkg))}`)
console.log(`Avro Size: ${Buffer.byteLength(buffer)}`)
assert.deepEqual(value, pkg)
fs.writeFileSync(path.join(__dirname, 'out', 'pkg-avro.dat'), buffer)
// JSON Size: 105
// Avro Size: 38