What is flatbuffers?
FlatBuffers is an efficient cross-platform serialization library for C++, C#, C, Go, Java, JavaScript, TypeScript, PHP, and Python. It allows you to create and access serialized data without parsing/unpacking it, making it ideal for performance-critical applications.
What are flatbuffers's main functionalities?
Creating a FlatBuffer
This code demonstrates how to create a FlatBuffer with a string and a vector of strings. The buffer is then finished and converted to a Uint8Array.
const flatbuffers = require('flatbuffers');
const builder = new flatbuffers.Builder(1024);
// Create a string
const str = builder.createString('Hello, FlatBuffers!');
// Create a vector of strings
const strings = builder.createVectorOfStrings(['foo', 'bar', 'baz']);
// Finish the buffer
builder.finish(strings);
// Get the buffer
const buf = builder.asUint8Array();
console.log(buf);
Reading a FlatBuffer
This code demonstrates how to read a FlatBuffer. It assumes that the buffer contains data for a 'Monster' object, and it accesses the 'name' and 'hp' fields of the object.
const flatbuffers = require('flatbuffers');
const MyGame = require('./mygame_generated'); // Generated code
// Assume buf is a Uint8Array containing the FlatBuffer data
const buf = new Uint8Array([/* FlatBuffer data */]);
// Get the root object
const bb = new flatbuffers.ByteBuffer(buf);
const root = MyGame.Example.Monster.getRootAsMonster(bb);
// Access data
console.log(root.name());
console.log(root.hp());
Other packages similar to flatbuffers
protobufjs
Protobuf.js is a pure JavaScript implementation of Protocol Buffers, a language-neutral, platform-neutral, extensible mechanism for serializing structured data. Compared to FlatBuffers, Protobuf.js requires a parsing step to access data, which can be less efficient in performance-critical applications.
msgpack
MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON but it's faster and smaller. Compared to FlatBuffers, MessagePack is simpler but may not offer the same level of performance for complex data structures.
avsc
Avro is a data serialization system that provides rich data structures and a compact, fast, binary data format. The 'avsc' package is a pure JavaScript implementation of Avro. Compared to FlatBuffers, Avro is more focused on schema evolution and data interchange, while FlatBuffers is optimized for performance.
FlatBuffers in JavaScript
This is an implementation of FlatBuffers in pure JavaScript. Unlike the official compiler, this implementation generates JavaScript code to convert between JavaScript objects and FlatBuffers at run time using the JIT. It currently requires binary schemas compiled with the flatc
compiler, which is written in C++ and has to be built (see the build instructions).
Example Usage
-
Create a schema called example.fbs
:
table Example {
x: float;
y: float;
}
root_type Example;
-
Generate the binary schema called example.bfbs
:
flatc --binary --schema example.fbs
-
Install this library:
npm install flatbuffers
-
Use the library:
var flatbuffers = require('flatbuffers');
var fs = require('fs');
var example = flatbuffers.compileSchema(fs.readFileSync('example.bfbs'));
var generated = example.generate({ x: 1, y: 2 });
var parsed = example.parse(generated);
console.log('generated:', Array.from(generated));
console.log('parsed:', parsed);
Running that code should print this:
generated: [ 12, 0, 0, 0, 8, 0, 12, 0, 8, 0, 4, 0, 8, 0, 0, 0, 0, 0, 0, 64, 0, 0, 128, 63 ]
parsed: { x: 1, y: 2 }