Binary Formats Are Better Than JSON in Browsers
a year ago
- #binary-encoding
- #web-development
- #performance
- JSON is no longer the fastest option for browser-based web apps; alternatives like Avro, Protobuf, or Bebop should be considered for performance-sensitive applications.
- Benchmarking binary encodings in browsers revealed that many alternatives to JSON, including MessagePack, were slower, but recent developments have improved their performance.
- Common benchmarking mistakes include using Node.js instead of browsers and focusing on numeric-heavy datasets, which don't reflect typical use cases involving strings.
- End-to-end latency measurements, including server request to client processing, provide a more accurate comparison than just deserialization times.
- Compression reduces message size differences but doesn't eliminate the extra processing time required for JSON in browsers.
- Schema-based encodings like Avro and Protobuf offer built-in validation, adding safety without significant performance overhead.
- Lazy decoding in libraries like Flatbuffers and Cap'n Proto can improve performance when not all data is accessed, but materializing full objects may be slower.
- Recent browser improvements and library updates have made binary encodings more viable, with Bebop, Avro, and Protobuf showing strong performance.
- JSON has limitations, such as string size caps, lack of type support (e.g., 64-bit integers), and no built-in validation, which can lead to runtime errors.
- TypeScript and libraries like zod can mitigate some JSON issues, but schema-based encodings provide better end-to-end safety.