UUIDs (Universally Unique Identifiers) are 128-bit values used to identify resources without central coordination. Whether you're building a REST API, syncing data across microservices, or generating primary keys for a distributed database, JavaScript offers several robust ways to create and work with UUIDs. This guide covers every approach you'll need in production.
The Modern Way: crypto.randomUUID()
Since Node.js 19+ and all modern browsers (Chrome 92+, Firefox 95+, Safari 15.4+), you can generate a Version 4 UUID with zero dependencies:
This is the recommended approach for most use cases. The method is cryptographically secure, fast, and available in both the browser and Node.js without installing anything. It returns a lowercase, hyphenated string conforming to RFC 9562.
The uuid npm Package
When you need more than random UUIDs — deterministic name-based IDs, time-ordered sorting, or validation utilities — the uuid package (60M+ weekly downloads) is the go-to library:
Then use it in your code:
UUID Versions Explained
Not all UUIDs are created equal. Each version has a different generation strategy and set of trade-offs. Here's what matters in practice:
| Version | Source | Sortable | Deterministic | Best For |
|---|---|---|---|---|
| v1 | Timestamp + MAC | Partially | No | Legacy systems, audit logs |
| v4 | Random | No | No | General-purpose IDs (most common) |
| v5 | SHA-1 (namespace + name) | No | Yes | Consistent mapping (URLs, DNS) |
| v7 | Unix ms timestamp + random | Yes | No | Database PKs, time-ordered data |
Why UUID v7 Is Gaining Traction
UUID v7 (added in RFC 9562, 2024) embeds a Unix millisecond timestamp in the most significant bits, making IDs naturally sort in chronological order while remaining globally unique. This solves the biggest complaint about v4 in databases: random key distribution causing index fragmentation and poor write performance.
Validation and Parsing
Accepting UUIDs from user input or external APIs? Always validate. Here are two approaches:
With the uuid Package
With a Regex (No Dependencies)
Need to test your own UUID regex patterns? Our Regex Tester lets you validate patterns against sample UUIDs in real time.
UUIDs vs Auto-Increment IDs
Choosing between UUIDs and auto-incrementing integers is one of the most common architecture decisions. Here's when each makes sense:
- You have multiple databases or services generating IDs independently
- IDs are exposed in URLs and you want to prevent enumeration attacks
- You need to generate IDs client-side before writing to the database
- Merging data from different sources without conflicts is required
- You have a single database with straightforward sequential writes
- Storage efficiency matters (4 bytes vs 16 bytes per row)
- You need maximum query performance on primary keys
- Human-readable IDs are helpful (order #12345 vs order #a7f2d...)
Database Considerations
How you store UUIDs in your database significantly impacts performance. Random v4 UUIDs cause B-tree index fragmentation because inserts scatter across the tree. Here's how to handle it:
| Database | Recommended Type | Notes |
|---|---|---|
| PostgreSQL | uuid | Native 16-byte type, use gen_random_uuid() |
| MySQL | BINARY(16) | Avoid CHAR(36) — wastes 20 bytes per row |
| SQLite | BLOB or TEXT | No native UUID; store as 16-byte blob for perf |
| MongoDB | UUID (BSON Binary subtype 4) | Use the UUID() constructor in drivers |
PostgreSQL Example
Practical Patterns
Generate IDs in an Express API
Convert UUID to Bytes and Back
Working with binary representations is useful when you need to store UUIDs efficiently or convert between formats. You can also Base64-encode the bytes for compact URL-safe transport.
Quick Reference: Generating UUIDs
Useful Tools
Try these free developer tools to work with UUIDs and related formats: