High-performance UUID version 7 generator for .NET with time-ordering capabilities. Compatible with .NET Standard 2.0+ and .NET 6, 7, 8+
$ dotnet add package Dis.UuidA high-performance .NET library for generating UUID version 7 (UUIDv7) identifiers with time-ordering capabilities.
UUID version 7 is a time-ordered UUID variant that combines:
This design ensures:
dotnet add package Dis.Uuid
Or via Package Manager Console:
Install-Package Dis.Uuid
using Dis.Uuid;
// Generate a new UUID v7
Guid id = UuidV7.New();
Console.WriteLine(id); // e.g., 018f-4230-1234-7000-abcdef123456
// Use in your models
public class Order
{
public Guid Id { get; set; } = UuidV7.New();
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
// ... other properties
}
Span<byte> and ThreadLocal<RandomNumberGenerator> for optimal performanceUUID v7 generation is extremely fast and efficient:
// Benchmark example
var sw = Stopwatch.StartNew();
for (int i = 0; i < 1_000_000; i++)
{
var uuid = UuidV7.New();
}
sw.Stop();
Console.WriteLine($"Generated 1M UUIDs in {sw.ElapsedMilliseconds}ms");
One of the key benefits of UUID v7 is natural time ordering:
var id1 = UuidV7.New();
await Task.Delay(1); // Small delay
var id2 = UuidV7.New();
// id2 will always be lexicographically greater than id1
Assert.True(string.Compare(id1.ToString(), id2.ToString()) < 0);
Perfect for:
The library is fully thread-safe and can be used concurrently:
// Safe to use across multiple threads
var tasks = Enumerable.Range(0, 10)
.Select(_ => Task.Run(() =>
{
var ids = new List<Guid>();
for (int i = 0; i < 1000; i++)
{
ids.Add(UuidV7.New());
}
return ids;
}));
var results = await Task.WhenAll(tasks);
// All generated UUIDs will be unique
The library includes comprehensive tests covering:
Run tests:
dotnet test
Contributions are welcome! Please feel free to submit issues and pull requests.
This project is licensed under the BSD 3-Clause License - see the LICENSE.txt file for details.
Made with ❤️ for the .NET community