Millisecond-precision, time-prefixed unique IDs inspired by KSUID.
mksuid generates a base62-encoded ID that embeds the creation time and a random payload.
npm install mksuidimport mksuid, { parse } from "mksuid";
const id = mksuid();
// => base62 string
const decoded = parse(id);
// => {
// time: Date,
// payload: Uint8Array
// }You can also generate an ID for a specific date:
import mksuid, { parse } from "mksuid";
const date = new Date("2026-04-05T12:34:56.789Z");
const id = mksuid(date);
const decoded = parse(id);
console.log(decoded.time.toISOString());
// 2026-04-05T12:34:56.789ZGenerates a new ID.
- If
dateis omitted, the current time is used. - Randomness comes from
crypto.getRandomValues.
Decodes an ID generated by mksuid.
Returns:
time: the embedded timestamp as aDatepayload: the random payload as aUint8Array
Throws if the input is not a valid mksuid string.
An mksuid value encodes:
1byte: timestamp length marker6bytes: timestamp in milliseconds since the Unix epoch14bytes: random payload
The full 21-byte value is then base62-encoded.
This package expects a modern JavaScript runtime with:
crypto.getRandomValuesBuffer
That includes Bun and modern versions of Node.js.
bun install
bun test
bun run buildMIT