$ show ai/nanoid
Nano ID is a JavaScript library for generating tiny, secure, URL-friendly unique string IDs. It provides a drop-in replacement for UUID v4 with shorter IDs (21 characters vs 36) while maintaining similar collision probability. The library is designed for high performance, being 50% faster than native `crypto.randomUUID()`, and is extremely lightweight at 117 bytes minified and brotlied with zero runtime dependencies. The architecture is modular: the main entry point (`index.js`) exports `nanoid()`, `customAlphabet()`, `customRandom()`, `random()`, and `urlAlphabet`. It uses the Node.js `crypto` module (Web Crypto API in browsers) for hardware-random bytes, with rejection sampling to ensure uniform distribution across the alphabet. A pre-generated string pool optimization avoids per-character concatenation, significantly boosting throughput. A browser-specific variant (`index.browser.js`) uses `crypto.getRandomValues` directly, and a non-secure variant (`non-secure/index.js`) uses `Math.random()` for environments without hardware random generators. The library also includes a CLI (`bin/nanoid.js`) for generating IDs from the terminal. Key data flows: random bytes are obtained from the system's secure random generator, then mapped to alphabet characters using either a bitmask (for power-of-two alphabets) or rejection sampling (for arbitrary alphabets). The pool mechanism pre-generates a buffer of random IDs and serves them via cheap substring operations, refilling as needed. The code is extensively commented, documenting every optimization hack, and is covered by unit tests, distribution uniformity tests, and benchmarks against other ID generators. External dependencies are limited to development tools (testing, benchmarking, linting). The library supports modern Node.js (22, 24, 26+), browsers, and React Native via a polyfill. It has been ported to over 20 programming languages, ensuring consistent ID generation across client and server stacks.
The LLM identified 3 files worth highlighting for reuse:
Core implementation of Nano ID. Provides nanoid(), customAlphabet(), customRandom(), and random() functions. Uses hardware random generator with rejection sampling to ensure uniform distribution. Implements a pre-generated string pool for performance, avoiding per-character concatenation. Handles power-of-two alphabets with bitmask for speed. Includes safeguards against large ID sizes and negative sizes. The code is heavily optimized for size and speed, with documented hacks.
Browser-specific version of Nano ID that uses crypto.getRandomValues directly instead of Node.js Buffer. Provides the same API as index.js but adapted for browser environments. Demonstrates how to maintain API compatibility across different runtime environments while leveraging platform-specific APIs.
Non-secure variant of Nano ID that uses Math.random() instead of hardware random generator. Suitable for environments without crypto support or when security is not a concern. Provides nanoid() and customAlphabet() functions with the same interface as the secure version. Uses simple modulo-based character selection without rejection sampling, making it simpler but less uniform.