Scripts2026年5月16日·1 分钟阅读

Nano ID — Tiny Secure URL-Friendly Unique ID Generator

A compact 118-byte string ID generator for JavaScript that produces URL-safe, cryptographically strong identifiers without dependencies.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Nano ID Overview
通用 CLI 安装命令
npx tokrepo install 76937ee0-50fe-11f1-9bc6-00163e2b0d79

Introduction

Nano ID generates compact, URL-friendly, cryptographically secure unique identifiers in JavaScript. At only 118 bytes (minified and gzipped), it replaces UUID v4 in scenarios where shorter, safer IDs matter — from database primary keys to session tokens.

What Nano ID Does

  • Generates 21-character unique string IDs by default using a URL-safe alphabet (A-Za-z0-9_-)
  • Uses the Web Crypto API (or Node.js crypto) for true randomness, avoiding Math.random pitfalls
  • Supports custom alphabets and ID lengths for domain-specific needs
  • Provides a non-secure variant for performance-critical paths where collision risk is acceptable
  • Ships as an ES module with zero dependencies, keeping bundle size minimal

Architecture Overview

Nano ID relies on a uniform distribution algorithm that maps random bytes onto a custom alphabet without modulo bias. The default alphabet contains 64 characters, so each character encodes 6 bits of entropy — a 21-character ID provides 126 bits, exceeding UUID v4's 122 bits. The library detects its runtime (browser vs. Node.js vs. Deno) and selects the appropriate cryptographic random source automatically.

Self-Hosting & Configuration

  • Install via npm, pnpm, or yarn — no native addons needed
  • Import from 'nanoid' (secure) or 'nanoid/non-secure' (faster, no crypto)
  • Set a custom size: nanoid(10) for 10-character IDs
  • Use customAlphabet to define project-specific character sets (e.g., lowercase only)
  • Works in Node.js 18+, all modern browsers, Deno, Bun, and Cloudflare Workers

Key Features

  • 118 bytes gzipped — smallest secure ID generator available
  • Cryptographically strong random values by default
  • No modulo bias thanks to a rejection-sampling approach over the alphabet
  • Fully tree-shakeable ES module with named exports
  • Over 26,000 GitHub stars and 60 million weekly npm downloads

Comparison with Similar Tools

  • uuid — generates RFC-4122 compliant 36-character strings; Nano ID is shorter and URL-safe by default
  • cuid2 — collision-resistant and sortable; Nano ID is smaller and does not require monotonic ordering
  • shortid — deprecated due to predictability; Nano ID uses crypto-safe randomness
  • ulid — time-sorted 26-character IDs; Nano ID is not time-ordered but more compact
  • crypto.randomUUID() — native and fast for UUID v4; Nano ID wins on length and custom alphabets

FAQ

Q: Is Nano ID safe for database primary keys? A: Yes. With 21 characters at 64-symbol alphabet, the probability of collision is negligible even at billions of IDs, comparable to UUID v4.

Q: Can I make IDs shorter? A: Yes, pass a length argument like nanoid(12). Shorter IDs raise collision probability, so size it according to your dataset scale.

Q: Does it work in edge runtimes? A: It works in Cloudflare Workers, Deno Deploy, Vercel Edge Functions, and any environment exposing the Web Crypto API.

Q: How does performance compare to UUID? A: Nano ID benchmarks slightly slower than crypto.randomUUID() in Node.js but remains under 1 microsecond per call — fast enough for virtually all workloads.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产