UUID v4
UUID v4 is a purely random UUID with 122 bits of entropy. It’s the most widely compatible UUID format, supported by virtually all databases and systems.When to Use
Use UUID v4 when you need:- Maximum compatibility - Supported everywhere
- Random identifiers - No sequential patterns
- Standard format - RFC 4122 compliant
- Database foreign keys - Works in any system
Basic Usage
API Reference
Main Function
Generate a UUID v4 string using the browser’s native
crypto.randomUUID() when no options are provided (fastest path).Generate a UUID v4 string with custom random bytes.Options:
random?: Uint8Array- 16 bytes of random data. Note: Bytes at index 6 and 8 will be modified in-place to set version/variant bits.
uuidv4(options, buf, offset)
<TBuf extends Uint8Array>(options: UuidV4Options | undefined, buf: TBuf, offset?: number) => TBuf
Write UUID v4 bytes directly into a buffer at the specified offset.Parameters:
options- UUID generation options orundefinedbuf- Target buffer (must have at least 16 bytes available from offset)offset- Starting position in buffer (default: 0)
Static Methods
Convert a UUID string to a 16-byte
Uint8Array.Convert a 16-byte
Uint8Array to a UUID string.Validate that a value is a properly formatted UUID v4 string. TypeScript type guard.
Constants
The nil UUID (all zeros):
"00000000-0000-0000-0000-000000000000"The max UUID (all ones):
"ffffffff-ffff-ffff-ffff-ffffffffffff"Real-World Examples
Database Entity IDs
Request Tracking
Bulk Generation with Buffer
Binary Storage
Type Definitions
Performance Characteristics
Generation Speed
Extremely fast - uses native
crypto.randomUUID() when no options providedEntropy
122 bits of cryptographically secure randomness
Size
36 characters (string) or 16 bytes (binary)
Collision Risk
Negligible - 1 in 2^122 chance
Bundle Size: ~940 bytes minified + gzipped
Validation Pattern
UUID v4 must match this pattern:- 36 characters total (32 hex + 4 hyphens)
- Version nibble (13th character) is always
4 - Variant bits (17th character) is always
8,9,a, orb
