New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

tinybuf

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tinybuf

Fast, compressed binary serializers in Node.js and HTML5

latest
Source
npmnpm
Version
1.8.8
Version published
Weekly downloads
22
22.22%
Maintainers
1
Weekly downloads
 
Created
Source

🔌 tinybuf  NPM version Minzipped Downloads Tests License

tinybuf icon showing binary peeking out from behind a square.

⚡Fast, compressed binary serializers in Node.js and HTML5

🔮 Simple, declarative API🔥 Blazing fast serialization
🗜️ Powerful compression💾 ^50% smaller than FlatBuffers
🍃 Zero dependencies🙉 Strong, inferred types
🌐 Node / browser🛡️ Built-in validation/transforms
🤏 ~4.4kb minzipped✅ Property mangling (Terser)

💿 Install

npm install tinybuf

Basic Usage

import { defineFormat, Type } from 'tinybuf';

export const GameWorldState = defineFormat({
  frameNo: Type.UInt,
  timeRemaining: Type.Float16,
  players: [
    {
      id: Type.UInt,
      position: {
        x: Type.Float32,
        y: Type.Float32
      },
      joystick: {
        x: Type.Scalar8,
        y: Type.Scalar8
      },
      actions: Type.Bools // [jump, attack]
    }
  ]
});

Encode

Formats can then be encoded:

let bytes: Uint8Array = GameWorldState.encode({
  frameNo: 50,
  timeRemaining: 59.334,
  players: [
    {
      id: 1,
      position: { x: 123.5, y: 456.75 },
      joystick: { x: 0.75, y: -0.662 },
      actions: [ /* jump: */ true,
               /* attack: */ false ]
    }
  ]
});

bytes.byteLength
// 16

Or directly from objects:

let bytes: Uint8Array = GameWorldState.encode( world );

bytes.byteLength
// 16

Decode

To Object

Decode as a strongly-typed object.

let obj = GameWorldData.decode( bytes );
// { frameNo: number; timeRemaining: number; … }

In-place

Extract fields directly into an existing object (this minimizes memory footprint).

let obj: Decoded<typeof GameWorldData> = {} as any;

GameWorldData.decodeInPlace( bytes, obj );

Parser – Decoding registered formats

  • Register formats with .on(format, handler, options?)
  • Trigger format handlers with .processBuffer(bytes)
import { bufferParser } from 'tinybuf';

// register
const parser = bufferParser()
  .on(MyChatMessage, msg => myHud.showChat(msg))
  .on(GameWorldData, data => myWorld.update(data), {
    decodeInPlace: true, // `data` gets recycled
  });

// parse
parser.processBuffer( bytes );

Types

TypeJavaScript TypeBytesNotes
Intnumber1-4*A signed integer from -Number.MAX_SAFE_INTEGER to Number.MAX_SAFE_INTEGER.
Int8number1A signed integer from -128 to 127.
Int16number2A signed integer from -32,768 to 32,767.
Int32number4A signed integer from -2,147,483,648 to 2,147,483,647.
UIntnumber1-4#An unsigned integer from 0 to Number.MAX_SAFE_INTEGER.
UInt8number1An unsigned integerfrom 0 to 255.
UInt16number2An unsigned integer from 0 to 65,535.
UInt32number4An unsigned integer from 0 to 4,294,967,295.
Float64number8A 64-bit double-precision floating-point number.
Float32number4A 32-bit single-precision floating-point number.
Float16number2A 16-bit half-precision floating-point number.
Note: Low precision. Maximum effective range ±65,504.
BFloat16number2A bfloat16 format 16-bit half-precision floating-point number.
Note: Lowest precision. Same effective range as Float32.
Scalarnumber1A signed scalar between -1.00 and 1.00 (two decimal precision).
UScalarnumber1A scalar between 0.00 and 1.00 (two decimal precision).
Boolboolean1A boolean value.
Boolsboolean[]1An array/tuple of boolean values (1 - 28) encoded as a single byte.
BufferUint8Array1 + nAn ArrayBuffer or ArrayBufferLike.
Stringstring1 + nA string (UTF-8 encoded).
JSONany1 + nAny JSON encodable value (encoded as a string).
RegExpRegExp2 + nJavaScript RegExp object.
DateDate8JavaScript Date object.

📘 Documentation

🏁 Quick start:Quick start guide,
Types
📑 Advanced:Async safety mode,
Format header collisions,
Compression tips,
Validation/transforms

Credits

tinybuf is based on Guilherme Souza's js-binary

Keywords

arraybuffer

FAQs

Package last updated on 07 Dec 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts