FlatBuffers vs JSON (Serialization compare)

Author:

Introduction

FlatBuffers and JSON are essential tools for data serialization, offering unique benefits. JSON is human-readable and widely used, while FlatBuffers enable faster, more efficient parsing with a compact binary format. Understanding their differences helps developers choose the right solution for optimizing performance and adaptability in modern applications.

What is Flattbuffers?

FlatBuffers is an efficient cross-platform serialization library by Google, enabling fast data access with minimal memory overhead by storing data in a flat binary buffer without parsing/unpacking.

It works with their on compiled classes, to make it work you’ll need to create a *.fbs file.

The example below is a fbs file with the a Table that represents the User entity.

Generate C# classes

.\flatc.exe --csharp flatbuffers.fbs

flatc.exe is the one responsible to generate classes based on fbs file.

And this is the User entity “converted” into FlatBuffers format. You should not make any changes on this file

Code Example

I have the User represented from a record that we’ll get from database.

Explaining image above

Red is a JSON data that will be used to deserialize and create the User entity.

Yellow is a Binary Data that will be used to deserialize and create the User entity.

Speed Compare

Results

This study demonstrates that FlatBuffers significantly outperforms System.Text.Json in deserialization speed, being 57 times faster. This stark performance difference highlights FlatBuffers’ efficiency, making it a superior choice for applications where rapid data processing and low latency are critical.

System.Net.Json took 115 ms while FlatBuffers was almost instantly

Code: https://github.com/rpbs/FlatBuffersJSON

Leave a Reply

Your email address will not be published. Required fields are marked *