msgpackr

Ultra-fast MessagePack serialization for JavaScript with record structures for faster, more compact encoding.

Library
npm
v2.1.0
688stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
64/100Good
Development Activity64
Maintenance44
Community60
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
69/100Good
Architecture78
Code Quality62
Innovation82
Learning Curve55

msgpackr is a MessagePack serialization library for JavaScript built for raw throughput, reporting encode speeds over 1.5GB/s and decode speeds over 2GB/s on Node.js — faster than native JSON.stringify/parse and other MessagePack implementations in its own benchmarks. Its optional record structure extension automatically detects repeated object shapes and encodes them once as a shared, referenced definition instead of re-writing every key on each object, often more than doubling decode throughput and shrinking payload size 15-50% versus plain MessagePack maps.

It runs anywhere modern JavaScript runs — Node.js, Deno, Bun, and browsers — with an optional native addon that accelerates string parsing on Node while falling back transparently to pure JS elsewhere. Beyond the basic pack/unpack functions, it ships Transform streams for socket and IPC use, structured-clone support for cyclic references and typed objects like Set, Map, and Error, sync/async iterator helpers, and a custom extension API for encoding arbitrary classes.

What You Get

  • Standalone pack/unpack functions for drop-in MessagePack compatibility with any standard decoder
  • Packr/Unpackr classes with record structures, cross-session shared structures, and structured cloning
  • PackrStream/UnpackrStream NodeJS Transform streams plus sync/async iterator helpers (packIter/unpackIter)
  • Universal builds for Node, Deno, Bun, and browsers, including a CSP-safe no-eval variant
  • A custom extension API (addExtension) for encoding arbitrary classes under a reserved type code

Common Use Cases

  • Serializing structured, similarly-shaped records (logs, telemetry, clinical data) for storage with shared structures
  • High-throughput IPC or socket messaging using PackrStream/UnpackrStream
  • Replacing JSON.stringify/parse in performance-sensitive Node hot paths
  • Browser-to-server binary protocols where bandwidth matters more than gzip-friendliness

Under The Hood

Architecture msgpackr’s core is two tightly coupled modules — pack.js and unpack.js — that share module-level mutable state (the working buffer, read/write position, and structure/record-transition tables) to keep the encode/decode hot path allocation-free; Packr extends Unpackr so a single instance can both pack and unpack while sharing that structure state. node-index.js/index.js are thin entry points that pick Node-native acceleration (the optional msgpackr-extract addon) versus a pure-JS fallback, while stream.js layers NodeJS Transform streams and iterators.js layers sync/async generators on top of the same Packr/Unpackr primitives rather than reimplementing serialization separately. It’s a deliberately mutable-global, single-purpose codec design optimized for throughput over strict encapsulation — changing the record/structure-transition logic in pack.js or unpack.js ripples through every consumer (streams, iterators, structured clone) since they all read the same shared state directly.

Tech Stack The library has no required runtime dependencies beyond the optional native addon msgpackr-extract for accelerated string extraction; it’s authored as ESM ("type": "module") and built to CJS/UMD with Rollup (@rollup/plugin-json, @rollup/plugin-replace, @rollup/plugin-terser), producing separate bundles for Node, browser, and a CSP-safe -no-eval variant via a string-replace pass that swaps out dynamic Function construction. Conditional exports map map node/bun/browser/types entry points per consumer, and typing is hand-authored (index.d.ts, pack.d.ts, unpack.d.ts) rather than compiler-generated. Tests run under Mocha with Chai assertions.

Code Quality The test suite is substantial — a 1500+ line Mocha/Chai file covering pack/unpack round-trips, record structures, and 32-bit float handling, plus dedicated files for streaming, async iterators, incomplete/partial buffer reads, and cross-runtime compatibility (Bun, Deno, and a separate compatibility-format test). Error handling favors explicit, descriptive thrown Errors (for example on exceeding the maximum shared-structure count) over silent failures, and the incomplete-read path is modeled explicitly through an error.incomplete flag with a lastPosition offset rather than a generic exception. That said, no CI workflow configuration or linter/formatter config is present in the repository, and the TypeScript declaration files are hand-maintained rather than checked against the implementation by a compiler.

API Design The standalone pack/unpack functions cover the common case with zero configuration, and encoder/decoder terminology aliases (encode/decode, Encoder/Decoder) ease the transition for developers coming from other codecs. The Packr/Unpackr classes progressively unlock record structures, persisted shared structures, and structured cloning without complicating the basic API, and the README documents every option in a single reference table. Persisted shared structures require a small amount of boilerplate (getStructures/saveStructures callbacks), but that’s inherent to the optimization rather than accidental API complexity.

Used by 5 apps in this directory

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search