Skip to content

Choosing the right type: TypeId vs. TypeIdDecoded

Mykhailo Matviiv edited this page Jul 22, 2023 · 1 revision

Overview

The library exposes two primary types for all operations involving TypeID: TypeId and TypeIdDecoded. While they're highly interchangeable and easily convertible from one to another, their optimization differs based on the usage scenario. This approach was taken to ensure that the performance expectations could be met depending on the requirement.

While both types are designed with performance in mind, striving to keep memory allocations as low as possible, it's crucial to select the type that aligns with your specific use-case in latency-sensitive applications.

Generally, TypeId is recommended for all use-cases, except for those that require access to the underlying components of TypeId.

Use-Cases and Performance

Here's a breakdown of common use-cases and how each type handles them.

Before diving into the table, here's a brief on the legend used:

  • βœ… - Indicates the type is optimized for this operation.
  • 🟑 - The operation is supported by this type, but it may underperform slightly compared to the other type.
  • ❌ - This type does not support the operation.
  • πŸ†“ - The operation is allocation-free, meaning it does not allocate any memory on the heap.
Operation TypeId TypeIdDecoded
Parse from string βœ… πŸ†“ ❌
Serialize to string βœ… πŸ†“ 🟑
Generate TypeId ❌ βœ…
Get string type 🟑 βœ… πŸ†“
Get Span type βœ… πŸ†“ βœ… πŸ†“
Get string suffix 🟑 🟑
Get Span suffix βœ… πŸ†“ 🟑 πŸ†“
Get timestamp ❌ βœ… πŸ†“
Match type βœ… πŸ†“ βœ… πŸ†“
Get UUIDv7 ❌ βœ… πŸ†“

Selecting the Appropriate Type

TypeId is generally the go-to choice for most operations, particularly for operations that involve dealing with sequences of characters (Span<char>) and serialization tasks. It shines in scenarios where parsing from a string or serializing to a string are key requirements, and you want to avoid memory allocation as much as possible.

On the other hand, TypeIdDecoded is more suited for situations where you need to extract or manipulate components of the TypeId, such as generating a new TypeId, retrieving the type as a string, or extracting the timestamp or UUIDv7. While it also handles operations on Span<char> and serialization tasks, it might perform slightly worse compared to TypeId.

In essence, the decision on which type to use heavily relies on your specific use case and the performance requirements of your application.

Clone this wiki locally