Last Updated: 3/9/2026
Introduction
Nano ID is a tiny, secure, URL-friendly, unique string ID generator for JavaScript.
import { nanoid } from 'nanoid'
model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"What is Nano ID?
Nano ID generates random, unique string identifiers that are:
- Compact: Just 21 characters by default (vs 36 for UUID)
- Secure: Uses hardware random number generation
- URL-safe: Only uses characters that don’t need encoding (
A-Za-z0-9_-) - Collision-resistant: Similar probability to UUID v4
Key Features
Tiny Size
At just 118 bytes (minified and brotlied), Nano ID is one of the smallest ID generators available. It has zero dependencies and uses Size Limit to enforce size constraints.
nanoid 118 bytes
customAlphabet 165 bytes
non-secure 90 bytesSecurity
Nano ID uses cryptographically strong random generation:
- Node.js: Uses the
cryptomodule’s hardware random generator - Browsers: Uses the Web Crypto API
- Uniform distribution: Custom algorithm ensures all characters have equal probability
See Security for detailed information.
Short IDs
Nano ID uses a larger alphabet than UUID (64 characters vs 16), allowing shorter IDs with the same collision probability:
- UUID v4: 36 characters using
0-9a-fand hyphens - Nano ID: 21 characters using
A-Za-z0-9_-
Both have ~126 bits of randomness, providing similar collision resistance.
Portable
Nano ID has been ported to over 20 programming languages, making it easy to generate consistent IDs across different platforms.
Comparison with UUID
Nano ID is comparable to UUID v4 (random-based) but with key advantages:
| Feature | Nano ID | UUID v4 |
|---|---|---|
| Size | 21 characters | 36 characters |
| Random bits | 126 | 122 |
| Alphabet | A-Za-z0-9_- (64 chars) | 0-9a-f + hyphens (16 chars) |
| Package size | 130 bytes | 423 bytes |
| URL-safe | Yes (no encoding needed) | No (hyphens need encoding in some contexts) |
Collision Probability
Both have similar collision resistance:
For there to be a one in a billion chance of duplication, 103 trillion version 4 IDs must be generated.
Use the collision probability calculator to evaluate safety for your use case.
When to Use Nano ID
Nano ID is ideal for:
✅ Database primary keys - Short, indexed keys improve performance
✅ URLs and slugs - URL-safe characters, no encoding needed
✅ API tokens - Secure random generation
✅ File names - Safe characters, no special encoding
✅ Session IDs - Cryptographically secure
✅ Distributed systems - No coordination needed between nodes
When NOT to Use Nano ID
❌ Sequential IDs - Nano ID is random, not sequential
❌ Sortable by time - Use ULID or KSUID for time-ordered IDs
❌ Human-readable codes - Use custom alphabets without ambiguous characters
❌ React keys - Use stable IDs from your data, not random generation
Performance
Nano ID is highly optimized but not the fastest option:
crypto.randomUUID 7,619,041 ops/sec
uuid v4 7,436,626 ops/sec
nanoid 3,693,964 ops/sec ← Good balance of speed and size
nanoid/non-secure 2,226,483 ops/sec ← Smaller, less secureBenchmarked on Framework 13 7840U, Fedora 39, Node.js 21.6
The performance difference is negligible for most applications. Nano ID’s advantages in bundle size and URL-safety often outweigh the speed difference.
What’s Next?
- Installation - Set up Nano ID in your project
- Quick Start - Generate your first IDs
- How It Works - Understand the internals