Skip to content

Getting Started with @briklab/net-t

@briklab/net-t provides compact binary serialization for arrays of numbers, optimizing both memory usage and transmission speed.

What is Bint?

Bint (Binary Integer) is a high-performance, proxy-driven array that stores numbers using the minimum byte count possible. Instead of storing every number as a 64-bit float (8 bytes each), bint dynamically selects the most efficient type for each value.

ts
import bint from "@briklab/net-t";

const instance = bint();
instance(42);        // Stored as Uint8 (1 byte)
instance(3.14);      // Stored as Float32 (4 bytes)
instance(1000000);   // Stored as Uint32 (4 bytes)

const buffer = instance.toBuffer();
console.log(buffer.byteLength); // Total: 9 bytes instead of 24

Basic Usage

Creating a Bint Instance

ts
import bint from "@briklab/net-t";

// Create a new instance
const data = bint();

// Add numbers (chainable)
data(255)        // Uint8
  (256)          // Uint16
  (70000)        // Uint32
  (3.14159)      // Float64
  (42)           // Uint8
  ;

console.log(data.length); // 5

Accessing Values

ts
import bint from "@briklab/net-t";

const data = bint();
data(10)(20)(30);

// Array-like access
console.log(data[0]); // 10
console.log(data.at(1)); // 20
console.log(data.at(-1)); // 30 (negative indices work)

// Iteration
for (const value of data) {
  console.log(value);
}

// Get all values
console.log(Array.from(data.values())); // [10, 20, 30]

Serialization

ts
import bint from "@briklab/net-t";

// Serialize
const data = bint();
data(42)(3.14)(1000);
const buffer = data.toBuffer();

// Deserialize
const restored = bint.fromBuffer(buffer);
console.log(restored.length); // 3
console.log(restored[0]); // 42
console.log(restored.at(1)); // 3.14

Live Demo

Console
No logs yet.

Supported Types

Bint automatically selects from these 10 numeric types:

Uint8 (1 byte)        → 0 to 255
Uint16 (2 bytes)      → 0 to 65,535
Uint32 (4 bytes)      → 0 to 4,294,967,295
BigUint64 (8 bytes)   → Large unsigned integers

Int8 (1 byte)         → -128 to 127
Int16 (2 bytes)       → -32,768 to 32,767
Int32 (4 bytes)       → -2,147,483,648 to 2,147,483,647
BigInt64 (8 bytes)    → Large signed integers

Float32 (4 bytes)     → Decimal numbers (limited precision)
Float64 (8 bytes)     → Decimal numbers (full precision)

Use Cases

  • Low-Latency Networking: Minimize bandwidth for real-time data
  • WebSocket Communication: Reduce payload sizes
  • IndexedDB Storage: Optimize client-side data storage
  • Binary Protocols: Implement efficient data formats
  • Performance-Critical Apps: Every byte counts

Features

  • Automatic Type Selection: Optimal type chosen per value
  • Array-like API: Familiar interface (indexing, iteration)
  • Zero Overhead: No unnecessary padding or metadata
  • Perfect Serialization: Reversible with toBuffer() / fromBuffer()
  • BigInt Support: Handle large numbers

Next Steps

Learn more about @briklab/net-t: