NanoID and UUID are both widely used unique identifier formats in web applications. They solve the same core problem — generating collision-resistant IDs without coordination — but differ in format, sortability, entropy, and ecosystem support. This guide compares UUID v7 (the RFC 9562 standard) against NanoID (from the NanoID GitHub repository) across the dimensions that matter for production systems.
At a glance
| Property | UUID v7 | UUID v4 | NanoID (default) |
|---|---|---|---|
| Example | 019236a7-b4f2-7000-8d3e-... | a8098c1a-f86e-4da4-bd1a-... | V1StGXR8_Z5jdHi6B-myT |
| Length | 36 chars (string), 16 bytes (binary) | 36 chars / 16 bytes | 21 chars |
| Entropy | 74 bits random + 48-bit timestamp | 122 bits | ~126 bits |
| Time-ordered | Yes | No | No |
| URL-safe | No (hyphens OK in paths, not query strings) | No | Yes |
| Standard | IETF RFC 9562 | IETF RFC 9562 | Community spec |
| DB native type | PostgreSQL uuid, SQL Server uniqueidentifier | Same | None (VARCHAR) |
| CSPRNG-backed | Yes | Yes | Yes |
Entropy and collision probability
Both UUID v4 and NanoID are effectively collision-proof for practical applications:
- UUID v4: 122 random bits → ~5.3 × 10³⁶ possible values
- NanoID (21 chars): ~126 bits → ~8.5 × 10³⁷ possible values
- UUID v7: 74 random bits + 48-bit timestamp → 74 random bits per millisecond window
For UUID v7, the collision probability resets each millisecond — within a single millisecond, only 74 bits of entropy separate two UUIDs with the same timestamp. In practice this is still astronomically safe (1 in 2⁷⁴ ≈ 1 in 18 quadrillion), but NanoID has slightly more entropy for purely random IDs. The NanoID npm package documentation includes a collision probability calculator.
Database performance
This is the decisive factor for primary keys at scale:
Both NanoID and UUID v4 cause B-tree index fragmentation on every insert — each new value lands at a random position in the sorted index. At 10 million rows, insert throughput degrades 3–5× compared to sequential keys.
UUID v7 eliminates this problem: time-ordered inserts always append to the end of the B-tree. The UUID v7 performance benchmarks show UUID v7 performance matching BIGSERIAL within 15% at 10 million rows on PostgreSQL.
| Primary key | Index fragmentation at 10M rows |
|---|---|
BIGSERIAL | Minimal |
| UUID v7 | Minimal |
| UUID v4 | High |
| NanoID | High (same as UUID v4) |
For database primary keys, UUID v7 is the clear winner. NanoID has no performance advantage over UUID v4 as a database key.
URL and API design
NanoID’s 21-character URL-safe format is ergonomically better for short links and human-facing identifiers:
UUID v7 in URL: /orders/019236a7-b4f2-7000-8d3e-9c1a2b3d4e5f (47 chars)
NanoID in URL: /orders/V1StGXR8_Z5jdHi6B-myT (28 chars)
However, UUID v7 can be base64url-encoded to 22 characters — nearly as compact as NanoID:
function uuidToBase64url(uuid) {
const hex = uuid.replace(/-/g, '');
const bytes = new Uint8Array(hex.match(/.{2}/g).map(b => parseInt(b, 16)));
return btoa(String.fromCharCode(...bytes)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
// "019236a7-b4f2-7000-8d3e-9c1a2b3d4e5f" → "AZI2p7TycACNPpwam8A=" (22 chars)
The UUID URL-safe encoding guide covers all compact UUID representations.
Ecosystem and tooling
UUID’s RFC status gives it native support across languages and databases:
- PostgreSQL: native
uuidtype (16 bytes),uuidv7()function in v18 - MySQL:
UUID()function,BINARY(16)storage - Java:
java.util.UUIDin standard library - C#:
System.Guid,Guid.CreateVersion7()in .NET 9 - Python: built-in
uuidmodule withuuid7()in 3.13+ - Go:
github.com/google/uuidon pkg.go.dev
NanoID has no standard library support in any language — always a third-party dependency. No database has a native NanoID type. If your stack spans multiple languages or you need interoperability with third-party systems that expect UUID format, UUID is the only viable choice.
When to use each
Use UUID v7 for:
- Database primary keys (B-tree performance)
- Event IDs, log entry IDs (time-ordered)
- Any system requiring interoperability with UUID-aware tooling
- IDs that appear in both database and API layer
Use NanoID for:
- Short, human-friendly codes (invite links, short URLs)
- IDs that must be URL-safe without encoding
- Client-side IDs where bundle size matters and no database is involved
- One-time tokens (though 256-bit random bytes are better for security)
Use UUID v4 for:
- Externally visible IDs where UUID format is expected but timestamp must not be disclosed
- Compatibility with systems that validate UUID format
Further reading
- UUID vs ULID — comparing UUID v7 to the other popular sortable ID
- UUID Security — what v7 discloses vs v4
- UUID v4 vs UUID v7 — full comparison
External references
- NanoID on GitHub
- NanoID on npm
- PostgreSQL UUID type
- RFC 9562 — UUID standard
- MDN crypto.getRandomValues()
Frequently asked questions
Is NanoID more secure than UUID v4?
NanoID and UUID v4 provide comparable security. The default NanoID (21 characters, alphabet of 64) has 126 bits of entropy — close to UUID v4's 122 bits. Both use a CSPRNG internally: NanoID uses crypto.getRandomValues() in browsers and crypto.randomFillSync() in Node.js. Neither should be used as a session token or secret key — use 256-bit random bytes for those. The NanoID GitHub repository documents its entropy calculation.
Can I use NanoID as a database primary key?
Yes, but with trade-offs. NanoID strings are URL-safe and compact (21 chars vs 36 for UUID), but they are not time-ordered — index performance degrades at scale identically to UUID v4. NanoID has no standard database column type (store as CHAR(21) or VARCHAR(21)), whereas UUID has native uuid column types in PostgreSQL, MySQL, and SQL Server. For database primary keys where index performance matters, UUID v7 outperforms NanoID at scale due to time-ordered inserts. The PostgreSQL UUID type documentation covers storage efficiency.
Is NanoID suitable for use in URLs?
Yes — NanoID's default alphabet (A-Za-z0-9_-) is URL-safe without percent-encoding. UUID v7 in URL paths requires the 36-character hyphenated form which is valid but verbose, or base64url encoding to compact to 22 characters. For short, URL-friendly IDs (slug-style), NanoID is ergonomically better. For database primary keys, API resource IDs where type checking matters, or any context requiring an RFC-standard format, UUID v7 is the better choice. See the RFC 3986 URI syntax specification for URL character rules.