MessagePack vs CBOR vs BSON: Byte Anatomy
I checked 453 vectors and 4,960 differential assertions across three decoders. Here is what the bytes prove—and what a successful parse cannot.
The byte 01 is not a format identifier. It is a complete MessagePack unsigned integer, a complete CBOR unsigned integer, and an invalid BSON document length. Ask an automatic detector what it is and the honest answer is “two things,” even though a dropdown may eventually show one.
That is the useful surprise behind this comparison. MessagePack, CBOR and BSON are not three difficulty levels of the same JSON encoder. They make different choices about markers, names, types, ordering and framing. A parse can be structurally perfect and still describe the wrong producer intent.
This is an empirical tour of DevKit's existing combined Binary Format Decoder, not an announcement of three new products. It already decodes all three formats in one tree-and-hex inspector.
What I actually tested
I reran npx tsx tmp/binary-validation.ts after inspecting its imports. The harness calls the repository parser and independent decoders: @msgpack/msgpack, cbor2 and bson, installed under tmp/binary-ref. Valid vectors check byte consumption, contiguous spans, recursive bounds, types, warnings and normalized typed JSON. Invalid vectors check rejection and diagnostics. Auto vectors compare complete candidates with an independent “does this library decode the whole input?” oracle.
| Suite | Vectors | Checks |
|---|---|---|
| MessagePack | 115 | 1,391 |
| CBOR | 151 | 1,779 |
| BSON | 174 | 1,712 |
| Auto-detection | 13 | 78 |
| Total | 453 | 4,960 |
Exact output from the rerun on 2026-11-27:
Independent binary parser validation
Node v24.22.0; references: @msgpack/msgpack 3.1.3, cbor2 2.4.2, bson 6.10.4
Vectors: 453 (msgpack=115, cbor=151, bson=174, auto=13)
Checks: 4960 (msgpack=1391, cbor=1779, bson=1712, auto=78)
Failures: 0
PASS: all independent checks passed
These are differential checks against independent decode libraries, not universal proof. The corpus is finite, some semantic checks deliberately allow a reference library to accept input that DevKit flags, and normalization is shared test logic. Zero failures means these 4,960 assertions agreed; it does not prove every possible byte sequence, every producer's intent, or universal interoperability.
One record, three grammars
For a concrete anchor, a separate read-only spot-check encoded {a: 1} with the three pinned libraries and decoded the results with DevKit:
| Format | Exact hex | Bytes | Typed JSON |
|---|---|---|---|
| MessagePack | 81 a1 61 01 |
4 | {"$map":[["a",1]]} |
| CBOR | a1 61 61 01 |
4 | {"$map":[["a",1]]} |
| BSON | 0c 00 00 00 10 61 00 01 00 00 00 00 |
12 | {"$document":[["a",1]]} |
All three consumed their entire input with zero errors. The 4/4/12 byte counts describe this record only. They are not a general size ranking, and this experiment measured no throughput or latency.
MessagePack: marker families, not a universal header
MessagePack's first byte usually selects both a type family and a compact width. Positive fixints are 00–7f; negative fixints are e0–ff, representing −32 through −1. The verified vector 7f decodes to 127, while e0 decodes to -32.
Longer values use dedicated markers. cd 01 00 is a big-endian uint16 containing 256. The maximum unsigned 64-bit vector is cf ff ff ff ff ff ff ff ff, decoded as 18446744073709551615 and wrapped in typed JSON rather than rounded into a JavaScript number. Multi-byte integers and floats are big-endian.
Text and binary are explicitly different families. a5 68 65 6c 6c 6f is fixstr "hello"; c4 03 00 7f ff is bin8 containing three arbitrary bytes. DevKit requires valid UTF-8 for strings but does not impose that rule on binary.
Extensions are a type identifier plus payload. The ext8 vector c7 03 07 01 02 03 means length 3, type 7, data 01 02 03. Fixext1/2/4/8/16 and ext8/16/32 provide other widths. Extension type −1 is the predefined timestamp form, with 4-, 8- or 12-byte payload variants.
One byte is intentionally unusable: c1 is reserved, described by the specification as “never used.” The parser reports reserved MessagePack marker 0xc1, consumes 0 of 1 byte and marks the result partial.
CBOR: major type plus additional information
CBOR splits the initial byte differently: the high three bits select major type 0–7, and the low five bits are additional information. Major types 0 and 1 are unsigned and negative integers; 2 and 3 are byte and text strings; 4 and 5 are arrays and maps; 6 is a tag; 7 covers simple values and floats.
Additional information 0–23 carries its argument inline. Values 24, 25, 26 and 27 introduce 1-, 2-, 4- and 8-byte arguments. Values 28–30 are reserved; 31 has context-dependent meaning. Thus 19 01 00 is a uint16 argument of 256, and 1b ff ff ff ff ff ff ff ff is the same maximum uint64 as above. Lengths and floating-point payloads are big-endian; CBOR also supports half, single and double precision.
Definite lengths are not the only option. Byte strings, text strings, arrays and maps can be indefinite-length, terminated by the break byte ff. Indefinite text and byte strings must use definite-length chunks of the same type. The verified bytes 5f 42 01 02 41 03 ff concatenate to binary 01 02 03; 9f 01 82 02 03 ff becomes [1,[2,3]].
Tags wrap one data item. Tag 0 is RFC 3339 text, tag 1 is epoch time, and tags 2 and 3 represent positive and negative bignums. The generic-tag vector d9 d9 f7 18 7b carries tag 55799 around integer 123. Simple values include false, true and null at f4, f5, f6, plus undefined at f7; f0 is simple value 16. These are not all ordinary JSON values.
BSON: named, ordered, length-framed documents
BSON starts a document with a signed little-endian int32 containing the total document length, including those four length bytes and the final terminator. Each element then contains a one-byte type, a NUL-terminated UTF-8 key cstring, and a type-specific value. A document ends with 00.
For the spot-check record, 0c 00 00 00 declares 12 bytes. Next, 10 selects int32, 61 00 is key "a", 01 00 00 00 is little-endian 1, and the final 00 terminates the document. Field names travel with the data, and the parser retains their order in $document key/value pairs.
BSON's types carry more document-specific meaning:
- int64 is signed, eight-byte, little-endian. The verified
9007199254740993vector becomes{"$int64":"9007199254740993"}inside its document wrapper. - Date is signed 64-bit milliseconds since the Unix epoch. The all-zero epoch payload becomes
{"$datetime":"1970-01-01T00:00:00.000Z"}. - BinData is an int32 payload length, a one-byte subtype, then the payload. Generic binary uses subtype 0 and exports as
{"$binary":"AAH/","subtype":0}for bytes00 01 ff. - Decimal128 occupies 16 bytes. The vector for
1.2300exports as{"$decimal128":"1.2300E+0"}; the harness separately checks that parsing this text reproduces byte-identical Decimal128 data.
A BSON document is not the whole input by definition. Consecutive documents are legal. The concatenation vector occupies 48 bytes and decodes to three items: {"$document":[["a",1]]}, {"$document":[["b","two"]]} and {"$document":[["c",[3]]]}. Each document's length establishes the next boundary.
Typed JSON: exact comparisons, not lossy prettifying
The inspector's JSON is an inspection report, not a universal inverse serializer. Maps use $map pair arrays so non-string keys and duplicate keys survive; BSON documents use $document pairs to preserve names and order. Integers outside JavaScript's safe range become decimal strings inside explicit wrappers.
| Concept | MessagePack vector → typed JSON | CBOR vector → typed JSON | BSON vector → typed JSON |
|---|---|---|---|
| Binary | c403007fff → {"$binary":"AH//"} |
4401020304 → {"$binary":"AQIDBA=="} |
1000000005760003000000000001ff00 → {"$document":[["v",{"$binary":"AAH/","subtype":0}]]]} |
| Unsigned 64-bit maximum | cfffffffffffffffff → {"$uint64":"18446744073709551615"} |
1bffffffffffffffff → {"$uint64":"18446744073709551615"} |
No unsigned uint64 BSON type; int64 is signed |
| Unsafe signed integer | int64 wrapper when outside safe range | $int64 for unsafe negative integers; $bignum for tags 2/3 outside safe range |
10000000127600010000000000200000 → {"$document":[["v",{"$int64":"9007199254740993"}]]]} |
| NaN | ca7fc00000 → {"$float":"NaN"} |
fa7fc00000 → {"$float":"NaN"} |
10000000017600000000000000f87f00 → {"$document":[["v",{"$float":"NaN"}]]]} |
| Infinity | ca7f800000 → {"$float":"Infinity"} |
fa7f800000 → {"$float":"Infinity"} |
10000000017600000000000000f07f00 → {"$document":[["v",{"$float":"Infinity"}]]]} |
| Extension or tag | c70307010203 → {"$ext":7,"$data":"AQID"} |
d9d9f7187b → {"$tag":"55799","value":123} |
No generic equivalent; BinData subtype is not an extension registry |
Negative zero is also explicit: {"$float":"-0"}. CBOR undefined becomes {"$undefined":true}, and simple value 16 becomes {"$simple":16}. These wrappers prevent a display layer from silently turning special values into null.
Auto-detection is a candidate list, not a verdict
Auto mode runs all three parsers. A candidate is complete only when the input is nonempty, every byte is consumed, there are no errors and the result is not partial. Multiple complete candidates set the ambiguity flag.
The verified 01 sample produces two complete candidates: MessagePack and CBOR, each consuming 1 byte with one item and no diagnostics. BSON is incomplete, consuming 0 bytes with one error. The displayed result is MessagePack, but the ambiguity flag remains true. That choice comes from deterministic ranking after completeness and distinctiveness checks, not from knowledge of the sender.
Other overlaps are real: 61 61 is both MessagePack fixstr "a" and CBOR text "a"; a1 01 02 is a one-entry map in both. Conversely, 90 is an empty MessagePack array but not a complete CBOR item. A valid parse proves grammatical compatibility under one reader's rules—not producer intent. When the transport or application tells you the format, pin it manually.
Failures worth inspecting
The invalid vectors are as important as the successful ones:
- MessagePack
01 c1decodes the first integer, then stops at the reserved marker with an error at offset 1. - CBOR
1creportsreserved CBOR additional information 28;ffoutside an indefinite container reports an unexpected break. - BSON
20 00 00 00claims 32 bytes but supplies 4, so the parser reports an overrun of 28 bytes. - Proper prefixes of representative objects test truncation across all three formats, including strings, arrays, maps, extensions, tags and nested BSON values.
- Duplicate MessagePack and CBOR map keys remain visible and generate warnings instead of being silently collapsed.
- Invalid UTF-8, mixed indefinite-string chunk types, missing breaks, bad BSON lengths and non-NUL-terminated strings produce explicit errors.
A diagnostic offset is evidence about where a reader stopped. It is not proof that the bytes were produced by that format and merely damaged.
Limits from the implementation
The shared decoder caps input at 2,000,000 bytes, nesting at 256, nodes at 50,000, top-level items at 20,000 and container items at 50,000. It stores at most 1,000 diagnostics. The UI pages tree rows, item cards and hex output rather than treating those page sizes as data limits; the typed-JSON preview is capped at 65,536 characters, while copy and download use the full JSON.
Parsing and export happen in the browser. Inputs are not uploaded or automatically written to the address bar. Actively creating a share link includes the original input, with a 6,000-byte share limit, so sensitive captures should not be shared casually.
Which format should you choose?
| Need | Better starting point | Reason and caveat |
|---|---|---|
| Compact generic values with explicit text/binary separation | MessagePack | Simple marker families and extension types; confirm your ecosystem supports the extensions you use |
| Tagged data, rich map keys, streaming-friendly indefinite containers | CBOR | Standard tags, simple values and flexible lengths; applications still need tag agreements |
| Ordered named document fields with database-native values | BSON | Document framing, Date, ObjectId, Decimal128 and BinData; expect document-oriented overhead and driver semantics |
| Unknown captured bytes | No automatic winner | Compare complete candidates, then use protocol context or ask the producer |
None of these rows says “always smaller” or “always faster.” Those claims require representative payloads, encoder settings, runtimes and timing methodology. The evidence here establishes byte anatomy and parser agreement, not a benchmark.
Try the same vectors in the Binary Format Decoder. Switch between Auto and a pinned format, select a node to highlight its exact byte range, and export the typed JSON when you need an auditable inspection artifact.
References
- MessagePack specification
- RFC 8949: Concise Binary Object Representation
- BSON specification
- BSON FAQ
Environment: validation measured 2026-11-27 on Windows x64 with Node.js v24.22.0, @msgpack/msgpack 3.1.3, cbor2 2.4.2 and bson 6.10.4. The command was npx tsx tmp/binary-validation.ts. The {a: 1} spot-check used the same three encoder versions and DevKit's decodeBinary(); its exact hex, lengths, errors and typed JSON were printed and verified separately, without adding vectors to the 453-vector suite.