Why is JSON faster than BSON in node.js?

The question should not be Why is JSON faster than BSON? but Why is JSON faster than BSON in node.js?.

In most environments binary encodings like BSON, MessagePack or CBOR would be easier to encode than the textual JSON encoding. However javascript environments (like v8/node.js) are heavily optimized for JSON handling (because it's a subset of javascript). JSON de/encoding is probably implemented there in native code in optimized fashion directly in the JS VM. The javascript VMs are however not that optimized for representing and manipulating byte arrays (which is used by a BSON library). Nodes native Buffer type might be better than a pure JS array, but working with it (and doing for example the JS string (UTF16) -> UTF8 byte decoding in JS) is still slower then the inbuilt JSON serialization.

In other languages like C++ with direct byte array access and utf8 string types the results might be completely different.


I believe Node.js and most browsers are the exception.

The simple answer is the JSON parser/serializer/deserializer (i.e. V8) are extremely optimized and are written in C/C++. The BSON parser is written in JavaScript. But even if the parser is written native (and I believe BSON has one) JSON still will probably win given how optimized V8 is for JSON.

If you use a platform like Java or C# the BSON format is probably going to be faster.

See @Matthais247 who answered after me but much more completely.


I think you can't judge the performance only by looking at serialise/deserialize. You've simply chosen the wrong use-case for BSON. BSON shines in databases - where you can do do the calculations on the data, without the need to serialize them. Also storing and retrieving binary data such as images makes BSON more efficient, as you don't need to encode the data as Hex/BASE64 or similar.

Try to make some calculations directly retrieving/storing the values in JSON and BSON. But use random access (not always the same item), so that the chance it is optimized under the hood is small.