DEV Community

Oyetoke Toby
Oyetoke Toby

Posted on

Is Protobufs better than JSON?

Protocol buffers (Protobufs), is a binary format developed by Google to serialized data between different services.

So what do you think, is it faster than JSON?

Top comments (3)

Collapse
 
elasticrash profile image
Stefanos Kouroupis

There is no doubt it's faster, but the question is ...should I compare it with JSON? JSON can have any schema you want, it can be dynamic and it's easy to modify.
if the consumer app is a javascript based app it can make assumptions about the data structure or even extract the schema etc...whereas in protobufs you need a predefined schema, which you cannot carry with you and you need to share it with other consumer services beforehand.

Collapse
 
ahferroin7 profile image
Austin S. Hemmelgarn

Is it faster? Almost always, there's less protocol overhead, and the encoding and decoding is almost always less complex than for JSON.

The thing is though, does that actually matter? Unless you're sending pretty significant amounts of data, that efficiency difference is not likely to matter much for your use case, and JSON is generally easier to work with in most cases.

Personally, I prefer YAML over both unless efficiency really matters, because:

  • The short-form representation of YAML is marginally more efficient than JSON (it needs fewer quotation marks).
  • The long-form representation of YAML is easier for most people to read than JSON, making it a bit easier to debug.
  • YAML still supports custom data type representations like protobufs do.
Collapse
 
eli profile image
Eli Bierman

Between the two, I prefer the flexibility of JSON and not having to maintain separate schemas and generate language bindings like many of the Protobuf libraries require. If speed or size of the data transfer becomes an issue, I like MessagePack. It’s another binary serialization format like Protobuf, but it’s flexible like JSON and doesn’t require schemas like Protobufs do. It’s also similarly size efficient as Protobufs, and can be extended to support custom types more easily than either Protobufs or JSON in my opinion. 99% of the time JSON is perfect, but a realtime low-latency application like chat or gaming would be a great use case for Protobufs or MessagePack.