KSUID
KSUID (K-Sortable Unique Identifier) is a 160-bit identifier consisting of a 4-byte timestamp (seconds since KSUID epoch: May 13, 2014) and 16 bytes of cryptographically random payload. It’s encoded as a 27-character Base62 string.When to Use
Use KSUID when you need:- Time-ordered IDs - Sortable by creation time (second precision)
- High entropy - 128 bits of randomness per ID
- Compact Base62 format - URL-safe, alphanumeric only
- Distributed systems - Safe to generate across multiple machines
KSUID uses second-precision timestamps (unlike UUID v7 and ULID which use milliseconds). Choose KSUID if second-level granularity is sufficient for your use case.
Basic Usage
API Reference
Main Function
Generate a KSUID string with current timestamp (seconds) and random payload.
Generate a KSUID string with custom options.Options:
secs?: number- Timestamp in seconds since Unix epoch (defaults toMath.floor(Date.now() / 1000))random?: Uint8Array- 16 bytes of random data for the payload
ksuid(options, buf, offset)
<TBuf extends Uint8Array>(options: KsuidOptions | undefined, buf: TBuf, offset?: number) => TBuf
Write KSUID bytes directly into a buffer at the specified offset.Parameters:
options- KSUID generation options orundefinedbuf- Target buffer (must have at least 20 bytes available from offset)offset- Starting position in buffer (default: 0)
Static Methods
Convert a KSUID string to a 20-byte
Uint8Array.Note: Base62 is case-sensitive. ‘A’ (value 10) and ‘a’ (value 36) decode to different byte values.Convert a 20-byte
Uint8Array to a KSUID string.Extract the embedded timestamp from a KSUID string.Returns: Milliseconds since Unix epoch (for API consistency with
ulid and uuidv7).Note: KSUID only has second precision, so the returned value will always end in 000.Validate that a value is a properly formatted KSUID string. TypeScript type guard.Note: Both uppercase and lowercase letters are valid Base62 characters, but they represent different values.
Constants
The nil KSUID (all zeros):
"000000000000000000000000000"The max KSUID (maximum valid value):
"aWgEPTl1tmebfsQzFP4bxwgy80V"KSUID Epoch
KSUID uses a custom epoch: May 13, 2014 00:00:00 UTC (Unix timestamp: 1400000000) This allows KSUID timestamps to fit in 4 bytes (32 bits) while supporting dates far into the future:Real-World Examples
Database Primary Keys
Log Aggregation
Distributed Queue Messages
Time-Series Data
Binary Storage
Testing with Deterministic Output
Type Definitions
Structure
KSUID consists of 160 bits (20 bytes) encoded as 27 Base62 characters:- Bytes 0-3: 32-bit timestamp (seconds since KSUID epoch: May 13, 2014)
- Bytes 4-19: 128 bits of cryptographically secure randomness
Base62 Encoding
KSUID uses Base62 encoding with this alphabet:'A' (value 10) and 'a' (value 36) represent different values.
Performance Characteristics
Generation Speed
1.5× faster than
@owpz/ksuid npm packageSortability
Sortable by creation time (second precision)
Size
27 characters (string) or 20 bytes (binary)
Entropy
128 bits of cryptographically secure randomness
Bundle Size: ~1.0 KB minified + gzipped
Validation Pattern
KSUID must match this pattern:- Exactly 27 alphanumeric characters
- Both uppercase and lowercase letters are valid
- Base62 encoded
Comparison with Other Formats
vs UUID v7
KSUID: 27 chars, second precisionUUID v7: 36 chars, millisecond precisionChoose UUID v7 for millisecond accuracy
vs ULID
KSUID: 27 chars, second precision, Base62ULID: 26 chars, millisecond precision, Base32Choose ULID for millisecond accuracy
vs Nanoid
KSUID: Time-ordered, second precisionNanoid: Random, no timestampChoose KSUID for sortability
Migration Guide
From @owpz/ksuid
uniku/ksuiduses a functional API vs class-based- Uses
Uint8Arrayinstead of Node.jsBuffer timestamp()returns milliseconds (for API consistency with ulid/uuidv7)- 1.5× faster performance
Cloudflare Workers: By default, Cloudflare Workers “freezes” time during request handling.
Date.now() returns the same value for an entire request, so all KSUIDs generated within a single request will have the same timestamp (second precision).