In recent months, I’ve had to dive deep into serialization and its impact on system performance for professional reasons. While it may seem like a minor technical detail, choosing the right method for data exchange can have a significant effect on application efficiency, especially in distributed systems.
JSON is a standard for sending and receiving data over the network. Sending data over the network requires sending every character of JSON over the network and although it is very readable. At least, more than XML or any other format, it is not the one that sends the fewest bytes over the network.
In this article, I’ll compare three common approaches: JSON, Protocol Buffers (Protobuf), and Object Serialization (when both the client and server use the same language, such as Dart or Kotlin). I’ll also share a practical table summarizing estimated results based on size and speed.
JSON
JSON (JavaScript Object Notation) is a human-readable text-based format for structuring data. It is widely used due to its simplicity and compatibility with almost any programming language.
Example in JSON:
Advantages:
- Readable by humans, making debugging easier.
- Extremely compatible across different programming languages.
- Ideal for heterogeneous systems.
Disadvantages:
- Size: Includes redundant text labels, making it heavier.
- Speed: Parsing text is slower compared to binary formats.
- Computational Cost: Requires conversion at both the client and server.
Protocol Buffers (Protobuf)
Protobuf, developed by Google, is a binary serialization format that encodes data in a compact and efficient structure. It requires a predefined schema (.proto) for data modeling.
Example in Protobuf:
Advantages:
- Compact Size: Binary messages are much smaller than JSON.
- High Speed: Serialization and deserialization are optimized for performance.
- Efficiency: Ideal for systems exchanging large amounts of data.
Disadvantages:
- Requires a predefined schema, which adds maintenance overhead.
- Not human-readable, making debugging harder.
- Both client and server must support Protobuf.
Object Serialization
When the client and server share the same language, Object Serialization allows direct conversion of objects into a binary format using language-specific libraries.
Example in Dart (Serverpod):
Advantages:
- Efficiency: Comparable to Protobuf in size and speed.
- Ease of Use: No separate schema is needed; the code serves as the model.
- Speed: Avoids intermediate parsing steps like JSON.
Disadvantages:
- Limited Compatibility: Only works when the client and server share the same language.
- Tight Coupling: May depend on language or framework versions.
- If we want this binary serialization efficiency, we must check and ensure that the technology we use does not create a JSON-based encoding and decoding wrapper.
Example in Kotlin for KMP:
Comparative Table with Estimated Results
The data in the following table was generated by running serialization and deserialization benchmarks using Serverpod, alongside comparisons using Dart for both JSON and Protobuf. All tests were conducted under the same conditions to ensure fairness and accuracy.
To measure performance, we serialized and deserialized a dataset containing nested objects, lists, strings, and integers. The benchmarks involved 10,000 operations for each method, with results averaged across multiple runs to account for any anomalies.
Key details of the test setup:
- Dataset: A structured object with 100 fields, including a mix of primitives and nested elements.
- Hardware: The benchmarks ran on a modern machine with an AMD Ryzen 5 processor and 16GB of RAM.
- Tools: Serverpod for its native serialization, and Dart libraries for JSON and Protobuf implementations.
While JSON and Protobuf are widely recognized for their specific strengths, Serverpod introduces additional value beyond raw performance. It combines solid serialization capabilities with advanced features like code autogeneration, built-in monitoring tools, and an ORM—making it a well-rounded choice for backend development.
The table below summarizes the findings, highlighting not just the performance metrics but also the qualitative differences between these serialization methods.
Comments
Post a Comment