X Xerobit

NanoID vs UUID — Which Unique ID Generator Should You Use?

NanoID generates shorter, URL-safe unique IDs using a custom alphabet. UUID v4 is the standard 128-bit identifier. Here's when to use each and how they compare on size, speed,...

Mian Ali Khalid · · 5 min read
Use the tool
UUID Generator
Generate UUID v4 and v7 identifiers in bulk.
Open UUID Generator →

NanoID and UUID v4 both generate unique random identifiers, but they make different tradeoffs. UUID v4 is a 36-character standardized identifier universally supported across databases, libraries, and protocols. NanoID is a 21-character URL-safe string optimized for size in URLs, logs, and user-visible contexts.

Use the UUID Generator to generate standard UUID v4 identifiers.

Side-by-side comparison

UUID v4:  f47ac10b-58cc-4372-a567-0e02b2c3d479  (36 chars)
NanoID:   V1StGXR8_Z5jdHi6B-myT                 (21 chars, default)
PropertyUUID v4NanoID
Length36 chars (string) / 16 bytes (binary)21 chars (default, configurable)
Alphabet0-9a-f + dashesA-Za-z0-9_- (URL-safe)
StandardRFC 4122 (widely supported)Library-specific
Random bits122 bits~126 bits (at 21 chars)
URL-safeNo (dashes are fine, but not compact)Yes
Database native typeYes (UUID type in Postgres, MySQL)No (store as VARCHAR)
Human readabilityRecognizable formatOpaque string

NanoID alphabet and collision math

NanoID’s default alphabet contains 64 characters (A-Za-z0-9_-). At 21 characters:

Entropy = 21 × log₂(64) = 21 × 6 = 126 bits

UUID v4 has 122 bits of entropy. NanoID at 21 chars has slightly more entropy than UUID v4.

To calculate collision probability at custom lengths:

import { customAlphabet } from 'nanoid';

// NanoID collision calculator: https://zelark.github.io/nano-id-cc/
// At default settings (21 chars, 64 alphabet):
// - Generating 1,000 IDs/hour for 100 years:
// - Probability of ONE collision: ~0.000000001%

You can reduce the length significantly and maintain acceptable collision probability for your use case. A 10-character NanoID with 64-char alphabet has ~60 bits of entropy — sufficient for most app-level IDs.

Installing and using NanoID

npm install nanoid

JavaScript / Node.js

import { nanoid } from 'nanoid';

// Default: 21-char URL-safe ID
const id = nanoid();
// "V1StGXR8_Z5jdHi6B-myT"

// Custom length:
const shortId = nanoid(10);
// "IRFa-VaY2b"

// Custom alphabet:
import { customAlphabet } from 'nanoid';
const numbersOnly = customAlphabet('1234567890', 10);
numbersOnly();  // "4081596321"

const urlFriendly = customAlphabet('abcdefghijklmnopqrstuvwxyz0123456789', 12);
urlFriendly();  // "k3h8m2x9z0pl"

Synchronous vs async

// Async (uses system entropy — preferred):
import { nanoid } from 'nanoid';
const id = nanoid();  // Synchronous in Node.js (non-blocking anyway)

// For browser: nanoid uses Web Crypto API automatically
// For Node.js: uses crypto.randomFillSync

Python equivalent

# No official nanoid Python port, but secrets module achieves the same:
import secrets
import string

ALPHABET = string.ascii_letters + string.digits + '_-'

def nanoid(size=21):
    return ''.join(secrets.choice(ALPHABET) for _ in range(size))

print(nanoid())      # "V1StGXR8_Z5jdHi6B-myT" equivalent
print(nanoid(10))    # Short ID

When to use NanoID

URLs and slugs: Shorter IDs fit better in URLs without encoding. example.com/p/V1StGXR looks cleaner than example.com/p/f47ac10b-58cc-4372.

Log files: 21-char NanoIDs are easier to scan than 36-char UUIDs when tailing logs.

User-facing IDs: Short codes for orders, invitations, short links. Configurable alphabet lets you avoid confusing characters (0 vs O, 1 vs l vs I).

JavaScript-first projects: NanoID is a native JS library with excellent performance, no native module dependencies, and a tiny bundle size (~130 bytes minified+gzipped).

Custom character sets needed: NanoID lets you define the exact alphabet. UUID is fixed at hex + dashes.

When to use UUID v4

Databases with native UUID support: PostgreSQL has a UUID type; MySQL has UUID(). Native types use 16 bytes instead of 36 varchar bytes, and indexing is optimized.

Interoperability: UUID is an international standard (RFC 4122). Any language, framework, or database understands UUID. NanoID is library-specific.

Existing UUID-based systems: If your auth library, ORM, or framework uses UUID, don’t introduce NanoID just for new IDs. Mixing formats creates confusion.

Audit trails and compliance: Some compliance frameworks (GDPR audit logs, financial systems) expect standard UUID format in records.

Cross-system IDs: When an ID will be passed between multiple services, APIs, or teams, UUID’s universal recognition reduces friction.

Performance comparison

Both are fast; the difference is negligible for most applications.

// Benchmark (Node.js, M1 Mac):
// UUID v4 (crypto.randomUUID): ~28M/sec
// NanoID (default, 21 chars):  ~22M/sec
// NanoID (custom, 10 chars):   ~28M/sec

At these speeds, the choice should never be based on performance.

Combining both: use case-specific IDs

Some systems use UUIDs internally but NanoIDs externally:

// Internal primary key (UUID, optimized for database):
const internalId = crypto.randomUUID();
// "f47ac10b-58cc-4372-a567-0e02b2c3d479"

// External share code (NanoID, user-visible):
import { customAlphabet } from 'nanoid';
const shareCode = customAlphabet('ABCDEFGHJKLMNPQRSTUVWXYZ23456789', 8);
// "K3H8M2X9" — no confusable characters

This approach gives you database efficiency with UUID and user-friendly short codes with NanoID.

Other alternatives

ULID: Universally Unique Lexicographically Sortable Identifiers. 26-character, URL-safe, embeds a timestamp prefix for sorting. Better than UUID v4 for time-ordered records.

KSUID: K-Sortable Unique Identifier. 27-character, sortable, 32-byte with 4-byte timestamp. Stronger entropy than ULID.

UUID v7: New UUID version (RFC 9562) with millisecond timestamp prefix. Sortable like ULID but follows UUID format (36 chars, database-native).

Snowflake IDs (Twitter): 64-bit integers with embedded timestamp, worker ID, and sequence. Extremely compact, sortable, and high-throughput — used in distributed systems at scale.


Related posts

Related tool

UUID Generator

Generate UUID v4 and v7 identifiers in bulk.

Written by Mian Ali Khalid. Part of the Dev Productivity pillar.