⚡Fast, compressed binary serializers in Node.js and HTML5
🔮 Simple, declarative API | 🔥 Blazing fast serialization |
🍃 Zero dependencies | 🙉 Strong, inferred types |
🗜️ Powerful & performant compression | 💾 50% smaller vs FlatBuffers |
🌐 Node / browser | 🛡️ Built-in validation / transforms |
🤏 ~5kb minzipped |
✅ Property mangling (Terser) |
npm install tinybuf
import { defineFormat, Type } from 'tinybuf';
export const GameWorldData = defineFormat({
world: {
seqNo: Type.UInt,
time: Type.Float16
},
players: [
{
id: Type.UInt,
position: {
x: Type.Float32,
y: Type.Float32
},
input: {
move: Type.Scalar,
buttons: Type.Bools // [ jump, crouch ]
}
}
]
});
const bytes = GameWorldData.encode({ /*…*/ });
bytes.byteLength
// 17
Warning
Safe mode: encode(…)
optimizes performance and memory by encoding bytes
into a shared buffer, and returning a Uint8Array
pointer. Subsequent calls
are destructive and unsuitable for asyncronous workflows (e.g. Promises, Web Workers).
Enable safe encode to automatically copy bytes to a safe buffer instead:
myFormat.encode({ /*…*/ }, true )
setTinybufConfig({ safe: true })
import { bufferParser } from 'tinybuf'
// register formats
const parser = bufferParser()
.on(GameWorldData, (data) => myWorld.update(data))
.on(MyChatMessage, (chat) => myHud.showChat(chat));
// process data
parser.processBuffer(bytes)
Or individual:
const data = GameWorldData.decode(bytes);
🏁 Quick start |
🤔 Types table |
📑 Custom headers |
🗜️ Compression tips |
✨ Validation & transforms |
tinybuf is based on Guilherme Souza's js-binary