Why to consider FlatBuffers over JSON?

Technology CommunityCategory: AndroidWhy to consider FlatBuffers over JSON?
VietMX Staff asked 3 years ago

FlatBuffers are an efficient cross platform serialization library. They were originally created at Google for game development, and other performance-critical applications.

Why use FlatBuffers?

  • Access to serialized data without parsing/unpacking — What sets FlatBuffers apart is that they represent hierarchical data in a flat binary buffer, in such a way that they can still be accessed directly without parsing and unpacking, while also still supporting data structure evolution (forwards/backwards compatibility).
  • Memory efficiency and speed — The only memory needed to access your data is that of the buffer. It requires zero additional allocations (at least in C++. Other languages may vary). FlatBuffers are also suitable for use with mmap (or streaming), requiring only part of the buffer to be in memory.
  • Tiny code footprint — FlatBuffers require only small amounts of generated code, and just a single small header as the minimum dependency, which is very easy to integrate.
  • Strongly typed — Errors happen at compile time rather than manually having to write repetitive and error prone run-time checks. Useful code can be generated for you.

I have tried to parse 4 mb of JSON file with both FlatBuffer and JSON. FlatBuffer tooks 1–5 ms and JSON took around 2000ms.And no GC was called in android app during FlatBuffer use. But GC was called so many time while using JSON. So, UI was freezing in case of JSON (only option left with JSON is to parse it in background thread).