UUID v4 vs v7: Which One Should You Use?
UUID v4: Random
UUID v4 generates a completely random 128-bit identifier. There is no embedded timestamp, so two UUIDs generated at the same time on different machines will never be predictable relative to each other:
// Generated with crypto.randomUUID() in the browser
"3b241101-e2bb-4d7a-8702-9e0e2a0c5e83"
// Version nibble: 4 (xxxxx-4xxx)
// Variant bits: 10xx (RFC 4122 compliant)
Use UUID v4 when you need simple, globally unique identifiers and don't care about sortability — API request IDs, session tokens, or any non-database context.
UUID v7: Time-Ordered
UUID v7 embeds a Unix timestamp in the most significant 48 bits, followed by random data. This makes UUIDs sort chronologically by default, which is critical for database indexes:
Why v7 is Better for Databases
Most databases use B-tree indexes. When you insert UUID v4 values, they land at random positions in the index, causing page splits and write amplification. UUID v7 values arrive in timestamp order, so new rows append to the end of the index — dramatically improving insert performance:
| Property | UUID v4 | UUID v7 |
|---|---|---|
| Sort order | Random | Time-ordered |
| DB insert performance | Poor (random seeks) | Excellent (sequential append) |
| Index fragmentation | High | Minimal |
| Extractable timestamp | No | Yes |
| Collision risk | Negligible | Negligible |
Rule of thumb: Use UUID v7 as primary keys in databases. Use UUID v4 for everything else — API tokens, trace IDs, session identifiers.
Quick Code Comparison
// UUID v4 — pure randomness
const v4 = crypto.randomUUID();
// → "f47ac10b-58cc-4372-a567-0e02b2c3d479"
// UUID v7 — time-ordered (Node.js 22+)
const v7 = crypto.randomUUID({ version: 7 });
// → "0193a0e1-2f44-7c00-b4d8-9c3e6f1a2b5c"
// Check which version a UUID is
function uuidVersion(uuid) {
return parseInt(uuid[14], 16);
}
uuidVersion(v4); // 4
uuidVersion(v7); // 7
Generate UUIDs for your project?
Use the UUID Generator tool