MessagePack: The Binary JSON Alternative Your Node.js App Needs
A complete guide to smaller payloads, native binary support, and smarter data serialization for JavaScript developers
Photo by Nastya Dulhiier on Unsplash
MessagePack: The Binary JSON Alternative Your Node.js App Needs
A complete guide to smaller payloads, native binary support, and smarter data serialization for JavaScript developers
Hi everyone, this is Joss. Today, I’m going to introduce you to MessagePack — a binary serialization format that can significantly reduce your payload sizes and handle binary data natively.
I recently came across this excellent article from AlgoWing where a Java developer achieved 10× faster API responses by switching from JSON to MessagePack. That got me curious: what about JavaScript? As it turns out, the story is more nuanced in our ecosystem — and I think it’s worth understanding why.
If you’re comfortable with JSON (and let’s be honest, who isn’t?), MessagePack is easy to pick up. But before you switch, you need to understand where it actually helps and where it doesn’t. Let me give you the complete, honest picture.
What Is MessagePack?
MessagePack (often abbreviated as MsgPack) is a binary serialization format. Think of it as JSON’s faster, more compact cousin.
The official tagline says it best:
“It’s like JSON, but fast and small.”
Here’s what that means in practice:
- Binary format: Unlike JSON’s human-readable text, MessagePack encodes data in binary, which is much faster to parse
- Compact size: Payloads are typically 50–80% smaller than equivalent JSON
- Type-rich: Supports more data types than JSON, including binary data and timestamps
- Cross-platform: Works across virtually every programming language
The key insight is that MessagePack is semantically equivalent to JSON. You can convert any JSON object to MessagePack and back without losing information. This makes migration incredibly straightforward.
Why Should JavaScript Developers Care?
You might be thinking: “JSON works fine for me. Why bother?”
Fair question. Here’s when MessagePack becomes valuable:
1. Bandwidth-sensitive applications If you’re paying for data transfer or your users are on mobile networks, 20–30% smaller payloads add up fast.
2. Real-time applications Building a chat app, multiplayer game, or live dashboard with WebSockets? Smaller payloads mean lower latency and reduced bandwidth costs.
3. Binary data transfer Need to send images, files, or buffers? JSON requires Base64 encoding (+33% size). MessagePack handles binary natively.
4. Mobile clients Smaller responses = faster load times = happier users on slow connections.
5. Caching layers Smaller serialized objects mean more data fits in Redis or Memcached, reducing memory costs.
MessagePack vs JSON: A Visual Comparison
Let’s look at a typical user object:
const user = {
id: 12345,
name: "Alice Martin",
email: "alice@example.com",
isActive: true,
roles: ["admin", "editor"],
metadata: {
lastLogin: "2025-01-15T10:30:00Z",
preferences: {
theme: "dark",
language: "en"
}
}
};
As JSON (human-readable text):
{"id":12345,"name":"Alice Martin","email":"alice@example.com","isActive":true,"roles":["admin","editor"],"metadata":{"lastLogin":"2025-01-15T10:30:00Z","preferences":{"theme":"dark","language":"en"}}}
Size: ~200 bytes
As MessagePack (binary):
86 a2 69 64 cd 30 39 a4 6e 61 6d 65 ac 41 6c 69 63 65 20 4d 61 72 74 69 6e a5 65 6d 61 69 6c b1 61 6c 69 63 65 40 65 78 61 6d 70 6c 65 2e 63 6f 6d a8 69 73 41 63 74 69 76 65 c3 a5 72 6f 6c 65 73 92 a5 61 64 6d 69 6e a6 65 64 69 74 6f 72 a8 6d 65 74 61 64 61 74 61 82 a9 6c 61 73 74 4c 6f 67 69 6e b4 32 30 32 35 2d 30 31 2d 31 35 54 31 30 3a 33 30 3a 30 30 5a ab 70 72 65 66 65 72 65 6e 63 65 73 82 a5 74 68 65 6d 65 a4 64 61 72 6b a8 6c 61 6e 67 75 61 67 65 a2 65 6e
Size: ~156 bytes (22% smaller)
The difference becomes more dramatic with larger payloads. Arrays of objects, nested structures, and repeated keys benefit enormously from MessagePack’s efficient encoding.
How MessagePack Encoding Works
Understanding the encoding helps appreciate why it’s faster. Here are some examples:
Value JSON MessagePack Savings
true(4 bytes)0xc3(1 byte) 75%false(5 bytes)0xc2(1 byte) 80%null(4 bytes)0xc0(1 byte) 75%127(3 bytes)0x7f(1 byte) 67%"hello"(7 bytes)0xa5hello(6 bytes) 14%
Small integers (0–127) fit in a single byte. Booleans and null are always one byte. String overhead is minimal. These micro-optimizations compound into significant savings.
Getting Started with MessagePack in Node.js
Let’s get practical. The fastest and most popular library for Node.js is msgpackr.
Installation:
npm install msgpackr
Basic usage:
import { pack, unpack } from 'msgpackr';
// Your data (just like you'd use with JSON)
const data = {
userId: 42,
username: "joss_dev",
tags: ["typescript", "nodejs", "performance"],
verified: true
};
// Encode to MessagePack (returns Buffer)
const encoded = pack(data);
console.log('Encoded size:', encoded.length, 'bytes');
// Decode back to JavaScript object
const decoded = unpack(encoded);
console.log('Decoded:', decoded);
That’s it. If you’ve used JSON.stringify() and JSON.parse(), you already know how to use MessagePack.
Comparing Performance: A Real Benchmark
Let’s run an actual benchmark in Node.js:
import { pack, unpack } from 'msgpackr';
// Generate a realistic payload
const generatePayload = () => ({
users: Array.from({ length: 100 }, (_, i) => ({
id: i + 1,
name: `User ${i + 1}`,
email: `user${i + 1}@example.com`,
isActive: Math.random() > 0.5,
score: Math.floor(Math.random() * 10000),
tags: ['developer', 'nodejs', 'typescript'],
metadata: {
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
settings: { theme: 'dark', notifications: true }
}
}))
});
const payload = generatePayload();
const iterations = 10000;
// Benchmark JSON
console.time('JSON serialize + deserialize');
for (let i = 0; i < iterations; i++) {
const str = JSON.stringify(payload);
JSON.parse(str);
}
console.timeEnd('JSON serialize + deserialize');
// Benchmark MessagePack
console.time('MessagePack serialize + deserialize');
for (let i = 0; i < iterations; i++) {
const buf = pack(payload);
unpack(buf);
}
console.timeEnd('MessagePack serialize + deserialize');
// Size comparison
const jsonSize = Buffer.byteLength(JSON.stringify(payload));
const msgpackSize = pack(payload).length;
console.log(`\nJSON size: ${jsonSize} bytes`);
console.log(`MessagePack size: ${msgpackSize} bytes`);
console.log(`Size reduction: ${((1 - msgpackSize / jsonSize) * 100).toFixed(1)}%`);
Typical results on Node.js 20:
JSON serialize + deserialize: 12.371s
MessagePack serialize + deserialize: 14.671s
JSON size: 273228 bytes
MessagePack size: 223480 bytes
Size reduction: 18.2%
Wait — JSON is faster for serialization? Yes, and here’s why:
The JavaScript Reality Check
JSON.stringify() and JSON.parse() are native V8 functions written in highly optimized C++. They've been battle-tested and tuned for over a decade. Meanwhile, msgpackr is pure JavaScript — it simply can't compete on raw CPU performance.
So why use MessagePack at all? Because serialization speed isn’t the whole story.
Where MessagePack Actually Wins
The real gains come from what happens after serialization:
1. Network Transfer Time
// 18% smaller = 18% faster over the wire
const jsonSize = 273228; // bytes
const msgpackSize = 223480; // bytes
const saved = jsonSize - msgpackSize; // 49,748 bytes per request
// On a 10 Mbps connection:
// JSON: 218 ms transfer time
// MsgPack: 178 ms transfer time
// Savings: 40 ms per request
2. Bandwidth Costs
At scale, smaller payloads mean real money saved:
// 1 million requests/day × 50 KB savings = 50 GB/day
// 50 GB × 30 days × $0.09/GB (AWS) = $135/month saved
// And that's just one endpoint
3. Mobile & Slow Networks
For users on 3G or spotty WiFi, 18% smaller responses translate directly to faster perceived performance.
4. Binary Data (No Base64 Tax)
This is where MessagePack crushes JSON. Sending a 1 MB image:
// JSON with Base64: ~1.37 MB (33% overhead)
// MessagePack: 1 MB (zero overhead)
5. Redis/Cache Storage
Smaller serialized objects = more data fits in memory:
// 100,000 cached objects × 50 KB savings = 5 GB less RAM needed
The Honest Performance Summary

Bottom line: In JavaScript/Node.js, MessagePack’s value isn’t CPU speed — it’s bandwidth reduction, binary data support, and network efficiency. The original article I referenced was about Java, where MessagePack libraries are faster than JSON parsing. In our ecosystem, the trade-offs are different.
Working with Binary Data
Unlike JSON, MessagePack handles binary data natively:
import { pack, unpack } from 'msgpackr';
import { readFileSync } from 'fs';
// Include binary data directly
const payload = {
filename: 'avatar.png',
mimeType: 'image/png',
data: readFileSync('./avatar.png'), // Buffer
uploadedAt: new Date()
};
const encoded = pack(payload);
const decoded = unpack(encoded);
// decoded.data is a Buffer containing the original binary
With JSON, you’d need Base64 encoding, which increases size by ~33%. MessagePack stores binary data as-is.
Extended Types: Dates and Custom Objects
One of msgpackr's best features is built-in support for JavaScript Dates and other native types:
import { pack, unpack, Packr } from 'msgpackr';
// Dates work automatically!
const data = {
event: 'user_signup',
occurredAt: new Date(),
};
const encoded = pack(data);
const decoded = unpack(encoded);
console.log(decoded.occurredAt instanceof Date); // true
console.log(decoded.occurredAt.toISOString()); // Works!
When to Stick with JSON
MessagePack isn’t always the right choice:
- Debugging: JSON is human-readable. Use JSON in development, MessagePack in production
- Browser DevTools: Network tabs show JSON beautifully; binary is opaque
- Public APIs: External developers expect JSON. Offer it as default
- Small payloads: Under 1KB, the difference is negligible
- Config files: Human editing requires human-readable formats
A hybrid approach often works best: JSON for external/debug scenarios, MessagePack for performance-critical paths.
Summary: Key Takeaways
Let’s recap what makes MessagePack valuable in the JavaScript ecosystem:
- 20–30% smaller payloads = faster network transfers and lower bandwidth costs
- Native binary support without Base64 overhead (saves 33% on binary data)
- Easy integration with NestJS, Express, and frontend frameworks
- Backward compatible — support both formats during migration
- NOT faster for CPU serialization in Node.js (V8’s JSON is native C++)
- Best for: high-bandwidth apps, real-time features, binary data, mobile users
And voilà!
You now have an honest picture of MessagePack in the JavaScript ecosystem. Unlike in Java or other languages where MessagePack can be genuinely faster for serialization, in Node.js the story is more nuanced: you’re trading slightly slower CPU performance for significantly smaller payloads.
Is it worth it? That depends on your bottleneck. If you’re CPU-bound on a single server, stick with JSON. But if you’re bandwidth-bound, paying for data transfer, serving mobile users, or working with binary data — MessagePack is absolutely worth exploring.
If you enjoyed the article or found it useful, please consider giving it a clap 👏 (you can clap more than one time) and following me for more tutorials and insights on optimizing your development workflow. Keep exploring and experimenting.
메타데이터
- post_id
- d0c95db18aa4
- slug
- messagepack-the-binary-json-alternative-your-node-js-app-needs-d0c95db18aa4
- url
- https://medium.com/@joss-dev/messagepack-the-binary-json-alternative-your-node-js-app-needs-d0c95db18aa4
- canonical_url
- https://medium.com/@joss-dev/messagepack-the-binary-json-alternative-your-node-js-app-needs-d0c95db18aa4
- author_url
- https://medium.com/@joss-dev
- status
- ok
- fetched_at
- 2026-06-22 00:13:37