“Use a binary format, it is smaller and faster” is repeated more often than it is measured. This article takes one small document, writes it as JSON, CBOR, MessagePack and Protobuf, and shows the actual bytes. Every number here comes from the tools on this site, so you can reproduce it in a minute.
One document, four encodings
Take the document {"id":150,"name":"testing"}. As JSON text it is 27 bytes. The other three:
| Format | Bytes | Hex |
|---|---|---|
| JSON | 27 | the text itself |
| CBOR | 19 | a2 62 69 64 18 96 64 6e 61 6d 65 67 74 65 73 74 69 6e 67 |
| MessagePack | 19 | 82 a2 69 64 cc 96 a4 6e 61 6d 65 a7 74 65 73 74 69 6e 67 |
| Protobuf | 12 | 08 96 01 12 07 74 65 73 74 69 6e 67 |
Encode the document in the JSON to CBOR or JSON to MessagePack tools and you get the first two rows. Read the CBOR bytes left to right: a2 says “a map with two pairs”, 62 is “a text string of two bytes”, 69 64 is id, 18 96 is the integer 150 in one following byte, 64 is a four-byte string name, and 67 is a seven-byte string testing. MessagePack is the same idea with different markers: 82 is a map of two, a2 a string of two, and cc 96 an unsigned byte 150.
The Protobuf row is different in kind, and so is its size. 08 means field number 1 with a varint, 96 01 is the varint for 150, 12 means field number 2 with a length, 07 is that length and the rest is testing. The names id and name appear nowhere in those 12 bytes. They live in the .proto file that both sides have. That is the main difference between the families.
Self-describing against schema
CBOR and MessagePack are self-describing: the bytes carry the keys and the types, so any decoder can turn them back into JSON without extra information. The price is that every record repeats its key names. Protobuf is schema-based: the bytes carry only field numbers, so they are smaller but unreadable alone.
Paste the Protobuf bytes into the Protobuf decoder and it prints {"1": 150, "2": "testing"} with a warning that, without the schema, names are numbers and types are guesses. Turn on the annotation option and it shows what it saw on the wire: "1 (varint)": 150 and "2 (string)": "testing".
Binary is smaller, but check after compression
Raw size is the number people quote, so look at it, and then at what actually crosses the network, which is usually compressed. Here are the same data in three shapes, as bytes (gzip at level 9 in brackets):
| Data | JSON | CBOR | MessagePack |
|---|---|---|---|
{"id":150,"name":"testing"} |
27 (47) | 19 (39) | 19 (39) |
100 records {id, name, active} |
4,135 (about 520) | 2,671 (about 465) | 2,595 (about 465) |
| the integers 0 to 99 | 291 (about 160) | 178 (about 155) | 103 (about 125) |
Three lessons come out of that table:
- For a tiny document, gzip makes things worse. The gzip header costs more than it saves: 27 bytes became about 47.
- For repetitive records, compression closes most of the gap. The binary versions are about 35% smaller raw but only about 10% smaller compressed, because gzip already squeezes the repeated key names that the binary formats still carry.
- The encodings differ in their own small ways. MessagePack stores the integers 0 to 127 in one byte each, while CBOR does that only for 0 to 23 and needs two bytes for 24 to 255, which is why the list of integers is 103 bytes in MessagePack and 178 in CBOR.
So “binary is smaller” is true, and “binary is much smaller on the wire” often is not. Measure with your own payload and your own compression before you migrate.
What binary gives you besides size
Exact integers. The tools’ sample document has "id": 9007199254740993, one more than JavaScript’s largest safe integer. CBOR stores it as 1b 00 20 00 00 00 00 00 01, MessagePack as cf 00 20 00 00 00 00 00 01, both as an exact 64-bit unsigned integer. The CBOR to JSON decoder turns the bytes back into the same digits. The format never rounds it. A program that reads the value into a floating-point number does, which is the story in why JSON silently changes your big numbers.
Raw bytes. JSON cannot hold bytes, so binary data has to become text, usually Base64, which costs a third more space (see Base64 is not encryption). CBOR and MessagePack have a byte-string type and carry the bytes as they are.
Lengths count bytes, not characters. The sample’s name is Lan Nguyễn, ten characters. In CBOR it begins with 6c, a text string of twelve bytes, because ễ takes three bytes in UTF-8. When you read a hex dump of text with accents, expect the length to be larger than the character count.
The shortest float. The sample’s "score": 4.5 is written in CBOR as f9 44 80, a half-precision float of three bytes, because the format’s preferred encoding uses the shortest form that keeps the value exactly.
Protobuf details worth knowing
A Protobuf field starts with a key byte: the field number shifted left by three bits, plus a wire type. 08 is field 1, wire type 0 (varint). 12 is field 2, wire type 2 (length-delimited). Integers are varints: seven bits per byte, with the top bit set on every byte except the last, so 96 01 is 22 plus 128, which is 150.
Because the decoder cannot see the schema, several things stay ambiguous, and the decoder tool lets you choose how to read them:
08 01is the unsigned integer 1, but read as a ZigZag signed integer (sint32) it is −1. Same byte, different meaning.0d 00 00 80 3fis a 4-byte fixed field: as an integer it is 1065353216, as a float it is 1.12 02 08 01could be a two-byte string or a nested message with field 1 equal to 1. The tool guesses a nested message when the bytes parse as one.- Truncated input is reported precisely:
08 96ends in the middle of a varint, so the decoder says “Truncated: varint of field 1 at byte 1 ends before its last byte”.
The field number is the contract. You can rename a field freely, but changing its number breaks every reader, which is why .proto files carry them explicitly.
Which one to choose
| Best when | Watch out for | |
|---|---|---|
| JSON | humans read it, every language has a parser, debugging with logs | repeated keys, no bytes type, big-number precision in JavaScript |
| CBOR | standards and constrained devices (WebAuthn uses it), JSON’s shape in binary | fewer libraries than JSON, not human-readable |
| MessagePack | a drop-in compact format for caches and queues | not byte-compatible with CBOR, fewer standard types |
| Protobuf | many services sharing a contract, gRPC, evolving schemas | unreadable without the .proto, field numbers must stay stable |
Debugging checklist
- Write the bytes as hex first. Many “decoder bugs” are actually a Base64 or encoding step in front.
- Check the first byte.
a2,82and08already tell you which family you are looking at. - Decode with the tool that matches the format, and with the Protobuf decoder only when you know it is Protobuf.
- If a length looks wrong, count bytes, not characters.
- If the schema is available, trust it over the decoder’s guess.
Everything you paste into these tools is processed in your browser and never uploaded.
Frequently asked questions
Is a binary format always smaller than JSON?
Smaller before compression, usually; after compression the gap is much smaller, and for a tiny document the compressed form can be larger than the original. In the measurements in this article, 100 small records were about 35% smaller as CBOR or MessagePack, but only about 10% smaller once everything was gzipped. Measure on your own payload.
What is the difference between CBOR and MessagePack?
They are close relatives: both are self-describing binary encodings of JSON-like data. CBOR (RFC 8949) is an IETF standard with a registry of tags for types such as dates, big numbers and URIs, and is used in standards like WebAuthn. MessagePack is older and simpler, with a very wide set of libraries. They are not byte-compatible.
Why can't I read a Protobuf message without the .proto file?
The bytes contain field numbers and wire types, not field names, and a length-delimited field could be a string, raw bytes, a nested message or a packed list. A decoder can only guess. The same bytes 12 02 08 01 can be a two-byte string or a nested message, so the schema is the only authority.
Do these formats keep big integers exact?
Yes. CBOR and MessagePack store 64-bit integers as 8 bytes, so 9007199254740993 survives. What can lose precision is a JavaScript program that reads the value into a double, not the format.
Which one should I pick for a new API?
Start with JSON unless you have measured a problem: it is readable in every tool and log. Choose Protobuf when many services share a contract and you want schema checks and safe evolution, usually with gRPC. Choose CBOR or MessagePack when you want JSON's shape in compact binary, for example in constrained devices or caches.
More guides
- CIDR notation: why a /31 has two usable hosts, what a /32 is for, and the subnet mistakes to avoid
- Hashing is not encryption: what a hash can do, and why passwords need bcrypt
- Unix timestamps: seconds, milliseconds or microseconds, and the dates where they go wrong
- UUID v4, UUID v7 and ULID: which ID to use, and what is inside each
- Base64 is not encryption: what it does, and the bugs it causes
- Regex backtracking: how one innocent pattern can freeze a server (ReDoS)
- URL encoding pitfalls: %20 or +, double encoding, and the characters that break links
- Cron and time zones: why your 02:30 job runs twice or never
- CSV edge cases: quotes, newlines, delimiters and the leading zero
- Why JSON silently changes your big numbers (and how to stop it)
- JWT pitfalls: decoding a token is not verifying it
- YAML surprises: the Norway problem, octal numbers and version strings