DEV Community

Cover image for TurboSerial! High-Performance JS Serialization
Matias Affolter
Matias Affolter

Posted on

TurboSerial! High-Performance JS Serialization

A deep dive into TurboSerial on NPM, a new JavaScript serialization library that outperforms MessagePack and CBOR through SIMD-inspired design, and why it might be your next choice for performance-critical applications.

In the world of web development, especially in performance-critical applications, the way we handle data is paramount. Sending large JSON payloads can be a significant bottleneck. This is where binary serialization formats like MessagePack and CBOR have traditionally stepped in, offering more compact and faster alternatives.

But what if we could push the boundaries of performance even further?

Enter TurboSerial, a new JavaScript serialization library designed from the ground up for extreme speed, inspired by modern, low-level CPU optimizations.

We ran a comprehensive benchmark suite of over 110 tests comparing TurboSerial against the well-established MessagePack (v5) and CBOR (cbor-js). The results were eye-opening.

See For Yourself

We believe in transparency and verifiable results. You can run the exact same benchmark suite in your own browser and see the performance difference firsthand.

Run the interactive benchmark

The Verdict: Compatibility and Speed

When it comes to handling the full spectrum of JavaScript types, the difference is stark. While the established libraries struggle with modern features like BigInt, complex Maps, and circular references, TurboSerial handles them with ease.

Compatibility is only half the story. In performance benchmarks, TurboSerial consistently outperformed the others, especially in scenarios involving large, structured data.

For small and medium-sized JSON-like objects, TurboSerial was consistently the fastest. But its true power was revealed when handling large TypedArrays, where it was dramatically faster than the competition. This isn't magic; it's by design.

What Makes TurboSerial So Fast?

TurboSerial's performance advantage comes from architectural decisions inspired by cutting-edge, low-level libraries like simdjson. It leverages concepts that allow modern CPUs to process data in parallel.

1. SIMD (Single Instruction, Multiple Data)

At its core, SIMD is a CPU feature that allows a single instruction to be executed on multiple data points simultaneously. Think of it as a multi-lane highway versus a single-lane road. Instead of processing one number at a time in an array, TurboSerial's SIMD-optimized routines can process blocks of 4, 8, or even more numbers in a single CPU cycle. This provides a massive throughput advantage for numeric arrays and TypedArrays.

2. Byte Alignment

Modern CPUs are fastest when they can read data from memory addresses that are multiples of 4 or 8. Accessing misaligned data can cause the CPU to perform extra work, slowing down the entire process. TurboSerial is meticulous about aligning data in its internal buffers, ensuring that every read and write operation is as fast as the hardware allows.

3. Inspired by SIMD.js

While the original SIMD.js specification was put on hold, its polyfills and the underlying "branchless" design philosophy were a major inspiration. By designing algorithms that avoid conditional branching (if/else) and instead process data in predictable blocks, TurboSerial minimizes CPU pipeline stalls, keeping the processor fed with data and operating at maximum efficiency.

Getting Started with the API

TurboSerial maintains a simple and intuitive API, making it easy to drop into any project.

import TurboSerial from '@pixagram/turboserial';

const serializer = new TurboSerial();

// Serialize any JavaScript value
const data = {
  string: 'Hello, World!',
  number: 42,
  bigint: 123456789012345678901234567890n,
  array: [1, 2, 3, 4, 5],
  date: new Date(),
  map: new Map([['key', 'value']]),
  typedArray: new Float32Array([1.1, 2.2, 3.3])
};

const serialized = serializer.serialize(data);
const deserialized = serializer.deserialize(serialized);
Enter fullscreen mode Exit fullscreen mode

For power users, the library offers a rich set of configuration options to fine-tune its behavior for specific use cases.

Advanced Configuration

const serializer = new TurboSerial({
  compression: false,           // (Upcoming feature)
  deduplication: true,          // Store duplicate objects/strings once
  shareArrayBuffers: true,      // Share ArrayBuffer references
  simdOptimization: true,       // Enable SIMD-like optimizations for arrays
  detectCircular: true,         // Handle circular references
  memoryPoolSize: 65536         // Initial memory pool size in bytes
});
Enter fullscreen mode Exit fullscreen mode

Conclusion

While MessagePack and CBOR are excellent, established libraries, TurboSerial represents a step forward, leveraging modern hardware capabilities to offer superior performance and wider type compatibility. For applications where every millisecond and every data type counts, TurboSerial is a powerful new contender that is well worth considering for your serialization needs.

Top comments (0)