scala-ulid
scala-ulid is a library for ULID.
Installation
Add the following to your sbt build (2.11.x, 2.12.x, 2.13.x):
val version = "..."
libraryDependencies += Seq(
"com.chatwork" %% "scala-ulid" % version
)
Usage
val ulid = ULID.generate()
val str = ulid.asString
println(str) // 01EQWGKT1S68Y9YM5TV34RQVQA
ULID.parseULID(str).foreach{ uild =>
println(ulid) // 01EQWGKT1S68Y9YM5TV34RQVQA
}
val bytes = uild.toBytes
ULID.fromBytes(bytes).foreach { ulid =>
println(ulid) // 01EQWGKT1S68Y9YM5TV34RQVQA
}
Benchmark
https://github.com/chatwork/scala-ulid/blob/main/benchmark/src/main/scala/jmh/ULIDBenchmark.scala
- | airframe/airframe-control | sulky/sulky-ulid | chatwork/scala-ulid |
---|---|---|---|
Random number generation | Calling Random#nextDouble 16 times | Calling Random#nextLong twice (upper 48 bits useless random number generation) | Calling Random#nextBytes(10) only once |
Latency 95%tile(nsec) | 1038 (newULIDString=1024nsec) | 524 | 460 |
Latency Max(msec) | 1.329 (newULIDString=0.755msec) | 0.721 | 0.790 |
java.util.UUID#randomUUID
is 95%tile = 327nsec, max = 0.703msec