On this page
@std/ulid
Overview Jump to heading
Utilities for generating and working with Universally Unique Lexicographically Sortable Identifiers (ULIDs).
To generate a ULID use the ulid
function. This will generate a
ULID based on the current time.
import { ulid } from "@std/ulid";
ulid(); // 01HYFKMDF3HVJ4J3JZW8KXPVTY
ulid
does not guarantee that the ULIDs will be strictly
increasing for the same current time. If you need to guarantee that the ULIDs
will be strictly increasing, even for the same current time, use the
monotonicUlid
function.
import { monotonicUlid } from "@std/ulid";
monotonicUlid(); // 01HYFKHG5F8RHM2PM3D7NSTDAS
monotonicUlid(); // 01HYFKHG5F8RHM2PM3D7NSTDAT
Because each ULID encodes the time it was generated, you can extract the
timestamp from a ULID using the decodeTime
function.
import { decodeTime, ulid } from "@std/ulid";
import { assertEquals } from "@std/assert";
const timestamp = 150_000;
const ulidString = ulid(timestamp);
assertEquals(decodeTime(ulidString), timestamp);
Add to your project Jump to heading
deno add jsr:@std/ulid
See all symbols in @std/ulid on
What is a ULID? Jump to heading
A ULID (Universally Unique Lexicographically Sortable Identifier) is a 26-character string that serves as a unique identifier. It combines a timestamp with random data, making it both unique and sortable based on the time of creation. ULIDs are designed to be URL-safe and can be used in various applications, such as databases and distributed systems.
Why use @std/ulid? Jump to heading
Use when you need unique, time-sortable IDs that are easy to generate and share. Great for databases, distributed systems, and user-facing tokens.
Tips Jump to heading
- Use
monotonicUlid()
when you need strictly increasing values within the same millisecond. - You can extract the timestamp with
decodeTime(ulid)
to aid debugging.