The original link: https://medium.freecodecamp.com/why-consider-flatbuffer-over-json-2e4aa8d4ed07

Why, you might ask, should we use a new tool when we already have standard JSON and conversion libraries like GSON and Jackson?

Try FlatBuffers first, and you’ll see that it’s much faster than JSON.

What is FlatBuffers?

FlatBuffers is a highly efficient cross-platform serialization library that can be used in C++, C#, C, Go, Java, JavaScript, PHP, and Python. It was developed by Google for use in game development and other performance-oriented applications.

Why use FlatBuffers?

  • Access to serialized data without escaping/unpacking – FlatBuffers differs from other libraries in that it uses binary buffers to represent hierarchical data so that they can be accessed directly without escaping or unpacking, while also supporting data structure evolution (forward and backward compatibility).

  • Memory efficiency Fast speed – Only the buffer in memory is required to access data. It requires no extra memory allocation (at least in C++, subject to change in other languages). FlatBuffers are also suitable for use with MMAP or data streams, requiring only a portion of the buffer to be stored in memory. The speed of access is close to that of the original structure, with only a slight delay (a virtual function table called Vtable) to allow format upgrades and optional fields. FlatBuffers are suitable for projects that take a lot of time and space (memory allocation) to access and build serialized data, such as games and other performance-sensitive applications. Look at the benchmarks here.

  • Flexibility — With optional fields, you not only have great upgrade and rollback compatibility (especially important for older games, where you don’t have to upgrade all the data for each release), but also freedom in choosing what data to store and in designing data structures.

  • Lightweight code footprint – FlatBuffers requires only a small amount of generated code and a small header file that represents minimal dependencies, making it easy to integrate. See the reference page above for details.

  • Strongly typed – Compile to report errors without having to write repeated error-prone runtime checks yourself. It automatically generates useful code.

  • Ease of use – generated C++ code allows streamlined access and build code. There are also optional methods for diagram escape, JSON-like runtime string presentation, and so on. (The latter is faster and more memory efficient than the JSON escape library)

  • Code is cross-platform and has no dependencies – C++ code can run on any recent GCC /clang and VS2010. There are also build files for testing and examples (.mk files on Android, cmake files on other platforms).

Who uses FlatBuffers?

  • BobbleApp, India’s first textured App. We use FlatBuffers in BobbleApp to significantly improve the performance of App.

  • Cocos2d-x, the first open source mobile game engine, uses FlatBuffers to serialize all game data.

  • Facebook uses FlatBuffers for client-side server communication in the Android App. They wrote an article describing how FlatBuffers speed up content loading.

  • Google’s Fun Propulsion Labs makes extensive use of FlatBuffers in all their libraries and games.

How much has App performance improved?

  • Escape speed Escaping a 20KB JSON stream (which is about the return size of BobbleApp) takes 35ms over the UI refresh interval of 16.6ms. If we escaped JSON, we would drop frames (visually stuttering) while sliding because we had to load the cache from disk.

  • To initialize a JSON escape, you need to build the field map and then escape it, which takes 100ms to 200ms, significantly slowing down the App startup time.

  • Garbage collection creates a lot of small objects when escaping JSON, and in our experiments, escaping a 20KB JSON stream allocated about 100KB of instantaneous storage, putting a lot of pressure on Java memory collection.

FlatBuffers vs JSON

I tried to escape a 4MB JSON file using FlatBuffers and JSON.

FlatBuffers took 1-5ms and JSON took about 2000ms. While there was no GC in the Android App during the use of FlatBuffers, there were many GC’s when using JSON. The UI is completely stuck with JSON, so it can only be escaped in background threads for real use.

How do you use FlatBuffer?

I wrote an example on my GitHub that shows you how to use FlatBuffer in a step-by-step way.

Welcome to pay attention to my public number, will be used in the fragmented time brush dry goods!