IDGen.io

UUID vs NanoID: Length, Collision Risk, and When to Use Each

Updated: October 2026 · IDGen.io

UUID and NanoID are two of the most common ways to generate unique identifiers without a central authority. Both produce collision-resistant values suitable for primary keys, tokens and resource IDs, but they differ in format, length, ecosystem support and practical trade-offs.

This article compares UUID v4 and the default NanoID configuration so you can decide which fits a given project.

Quick comparison

Feature UUID v4 NanoID (default)
Length36 characters (with hyphens)21 characters
AlphabetHex (0-9, a-f)URL-safe (A-Za-z0-9_-)
Random bits122~126
Collision resistanceExtremely lowComparable to UUID v4
URL-safeNo (longer string)Yes
Formal standardRFC 9562 / RFC 4122Community library
Native DB typeYes (PostgreSQL, MySQL 8+)No (VARCHAR/TEXT)
JS bundle size0 (native crypto.randomUUID)~130 bytes gzipped
Time-sortableNo (use UUID v7)No

Length and storage

A UUID v4 is always 36 characters when displayed with hyphens (32 hex digits + 4 hyphens). NanoID defaults to 21 characters. The shorter form reduces storage in indexes, logs, URLs and JSON payloads. In high-volume tables or when IDs appear in every API response, the difference is measurable.

NanoID also lets you change the length. Shorter IDs lower entropy; longer ones raise it. The default 21-character setting was chosen to match or slightly exceed the collision resistance of UUID v4.

Collision probability

UUID v4 has 122 bits of randomness. NanoID’s default alphabet (64 characters) and length (21) yield approximately 126 bits. In both cases the probability of a random collision is negligible for any realistic system.

You would need to generate on the order of 10¹⁸ identifiers before the birthday-paradox risk becomes material. For almost all applications the difference in raw entropy is irrelevant; practical concerns (length, URL safety, library support) matter more.

URL safety and public identifiers

NanoID uses only characters that are safe in URLs and file names (A-Z, a-z, 0-9, _, -). A UUID contains hyphens; while hyphens are allowed in URLs, the longer string is less convenient for short links, invite codes or path segments.

If the identifier will appear in a public URL, a QR code, or a shareable token, NanoID is usually the cleaner choice.

Standards and interoperability

UUID is an official standard (RFC 4122, updated by RFC 9562). Every major language, database and cloud service understands it. PostgreSQL and recent MySQL versions offer a native UUID column type, which can be more efficient than a string column for indexing and storage.

NanoID is a library specification. Ports exist for many languages, but it is not a formal standard. Systems that must exchange identifiers with third parties, older systems, or multiple language stacks generally prefer UUID.

Generation speed and bundle size

In modern browsers and Node.js, crypto.randomUUID() is built-in and requires no extra dependency. The popular uuid package is larger. NanoID’s JavaScript implementation is extremely small (~130 bytes gzipped) and benchmarks show it is competitive with, or faster than, common UUID libraries.

For pure client-side applications where every byte counts, NanoID has a measurable advantage. On the server the difference is rarely decisive.

Database primary keys

Both formats can serve as primary keys. UUID benefits from native type support in several databases. Random UUIDs (v4) and random NanoIDs both produce non-sequential values, which can cause index fragmentation on large, write-heavy tables. If index locality matters, consider time-sortable alternatives such as UUID v7 or ULID instead of either UUID v4 or NanoID.

Practical recommendations

Prefer UUID v4 when:

  • identifiers cross system or language boundaries
  • you need a formal standard or native database UUID type
  • the project already standardises on UUIDs

Prefer NanoID when:

  • identifiers appear in URLs, tokens or short codes
  • you control both ends of the system and mainly work in JavaScript/TypeScript
  • shorter strings or a tiny client-side library are valuable

Many codebases use both: UUID for internal primary keys and NanoID for public-facing tokens and slugs.

Try them

You can generate both formats directly in the browser with no data leaving your device:

UUID vs NanoID:长度、碰撞风险与适用场景

更新于 2026 年 10 月 · IDGen.io

UUID 和 NanoID 是目前最常用的两种无需中心协调即可生成唯一标识符的方式。两者都能产生足够抗碰撞的值,可用于主键、令牌和资源 ID,但在格式、长度、生态支持和实际权衡上存在明显差异。

本文对比 UUID v4 与默认配置的 NanoID,帮助你根据项目需求做出选择。

快速对比

特性 UUID v4 NanoID(默认)
长度36 字符(含连字符)21 字符
字符集十六进制(0-9, a-f)URL 安全(A-Za-z0-9_-)
随机位数122约 126
碰撞抵抗力极低与 UUID v4 相当
URL 安全否(字符串更长)是
正式标准RFC 9562 / RFC 4122社区库
原生数据库类型支持(PostgreSQL、MySQL 8+)不支持(需用 VARCHAR/TEXT)
JS 包体积0(原生 crypto.randomUUID)约 130 字节(gzip)
按时间可排序否(可用 UUID v7)否

长度与存储

UUID v4 带连字符时固定为 36 个字符。NanoID 默认 21 个字符,在索引、日志、URL 和 JSON 载荷中能明显节省空间。高流量表或每次 API 响应都携带 ID 时,这个差异会被放大。

NanoID 支持自定义长度。更短会降低熵值,更长则提高。默认 21 字符的设计目标是达到或略超过 UUID v4 的碰撞抵抗力。

碰撞概率

UUID v4 有 122 位随机性。NanoID 默认使用 64 字符字母表和 21 长度,约提供 126 位熵。两者在实际系统中发生随机碰撞的概率都可以忽略不计。

需要生成约 10¹⁸ 量级的标识符,生日悖论带来的风险才会变得有意义。对绝大多数应用来说,原始熵值的细微差别并不重要,真正需要权衡的是长度、URL 安全性和库支持。

URL 安全与公开标识符

NanoID 只使用 URL 和文件名中安全的字符(A-Z、a-z、0-9、_、-)。UUID 含有连字符,虽然连字符在 URL 中通常合法,但更长的字符串对短链接、邀请码或路径段不够友好。

如果标识符会出现在公开 URL、二维码或可分享令牌中,NanoID 通常更合适。

标准与互通性

UUID 是正式标准(RFC 4122,后由 RFC 9562 更新)。几乎所有主流语言、数据库和云服务都原生支持。PostgreSQL 和较新的 MySQL 提供原生 UUID 列类型,在索引和存储上可能比字符串列更高效。

NanoID 属于库规范,虽有多语言移植,但并非正式标准。需要与第三方、旧系统或多语言栈交换标识符时,通常优先选择 UUID。

生成速度与包体积

现代浏览器和 Node.js 内置 crypto.randomUUID(),无需额外依赖。常用的 uuid 包体积更大。NanoID 的 JavaScript 实现非常小(gzip 后约 130 字节),基准测试显示其速度可与常见 UUID 库持平或更快。

纯客户端、对体积敏感的场景中,NanoID 有明显优势;在服务端差异通常可以忽略。

作为数据库主键

两种格式都可用作主键。UUID 在部分数据库有原生类型支持。随机的 UUID v4 和随机的 NanoID 都会产生非连续值,在高写入量的大表上可能导致索引碎片。如果索引局部性很重要,应考虑 UUID v7 或 ULID 等可按时间排序的方案,而不是 UUID v4 或 NanoID。

实际建议

优先选择 UUID v4 的情况:

  • 标识符需要跨系统或跨语言边界
  • 需要正式标准或原生数据库 UUID 类型
  • 项目已经统一使用 UUID

优先选择 NanoID 的情况:

  • 标识符会出现在 URL、令牌或短码中
  • 两端都由自己控制,且主要使用 JavaScript/TypeScript
  • 更短的字符串或极小的客户端库更有价值

很多项目会同时使用两者:内部主键用 UUID,对外公开的 token 和 slug 用 NanoID。

立即试用

以下生成器均在浏览器本地运行,数据不会离开设备:

Generate IDs instantly (100% browser-local) 立即生成 ID(100% 浏览器本地运行)

← Back to Blog ← 返回博客列表