-
Notifications
You must be signed in to change notification settings - Fork 4
Choosing the right type: TypeId vs. TypeIdDecoded
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.
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 | β | β π |
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.