opensourceprojects.dev

A broadsheet for software that doesn't ask for your email

Nano ID: 127 bytes, faster than crypto.randomUUID, no dependencies
GitHub RepoImpressions3

Project Description

View on GitHub

Nano ID: A Tiny, Fast Alternative to UUIDs for JavaScript

You need unique IDs for your database records, session tokens, or whatever else. You reach for crypto.randomUUID() or a UUID library, and it works fine. But then you notice your IDs are 36 characters long, they're not exactly fast to generate, and you're pulling in a dependency just to get them. What if there was something smaller, quicker, and just as safe? That's where Nano ID comes in.

What It Does

Nano ID is a tiny, secure, URL-friendly unique string ID generator for JavaScript. It's a drop-in replacement for UUID v4 in most cases, producing IDs like "V1StGXR8_Z5jdHi6B-myT" with a single function call.

The core idea is simple: instead of using the standard UUID alphabet and format, Nano ID uses a larger alphabet of A-Za-z0-9_- and packs a similar amount of randomness into just 21 characters instead of 36. It uses the crypto module in Node.js and the Web Crypto API in browsers, so you're getting hardware-backed randomness, not the unsafe Math.random(). The whole library is 127 bytes when minified and brotlied, with zero dependencies. It's been ported to over 20 programming languages, so you're not locked into JavaScript if your stack changes.

Why It's Cool

  • It's genuinely small. 127 bytes minified and brotlied. That's not a typo. For comparison, that's smaller than most individual functions you'd write yourself. The project uses Size Limit to keep it that way, so you won't wake up one day to a bloated dependency.

  • It's fast—faster than native. The benchmark in the README shows Nano ID hitting around 20.4 million operations per second, compared to 12.8 million for crypto.randomUUID() and 7.9 million for uuid/v4. That's roughly 50% faster than the native browser API, which is a rare thing to see from a userland library. The non-secure version is slower in the benchmark, interestingly, but the secure default is the one you'll use most of the time.

  • Shorter IDs without sacrificing safety. UUID v4 has 122 random bits; Nano ID has 126. So you're actually getting slightly more randomness in a string that's 15 characters shorter. The README notes that for a one-in-a-billion chance of duplication, you'd need to generate 103 trillion IDs. For practical purposes, collisions aren't something you'll worry about.

  • It handles the modulo bias problem. If you've ever written random % alphabet, you've introduced a subtle flaw where some characters appear less often than others. That reduces the effective randomness and makes brute-force attacks slightly easier. Nano ID uses a better algorithm and tests for uniformity, which is the kind of detail that separates a toy from a tool you can trust.

  • It's portable in a meaningful way. The 20+ language ports aren't just for show. If you're generating IDs on a Node.js backend and a mobile app, you can use the same ID format and characteristics across both. That's a small thing that saves friction.

How to Try It

Getting started is about as simple as it gets. Install it with npm:

npm install nanoid

Then import and use it:

import { nanoid } from "nanoid";
model.id = nanoid(); //=> "V1StGXR8_Z5jdHi6B-myT"

That's it. If you need a custom alphabet or size, the API supports that too. You can also supply your own random bytes generator if you have specific requirements, and there's a non-secure version available if you're in an environment where the crypto module isn't available (though the README is clear that you should use the secure version whenever possible).

The README covers usage in React, React Native, PouchDB, CouchDB, and even a CLI. There's also a TypeScript section, so if you're working in a typed codebase, you're covered.

For alternative install methods, you can grab it from JSR or a CDN. The full documentation and source are at github.com/ai/nanoid.

Final Thoughts

Nano ID is one of those libraries that does one thing and does it well. It's not trying to be a framework or a platform—it's just a better way to generate unique IDs. If you're currently using UUIDs and you care about bundle size, generation speed, or just want shorter IDs that are easier to read and log, it's worth trying. The fact that it's faster than the native API and smaller than almost anything else you'll install is a nice bonus. It's maintained by Evil Martians, a consultancy that works on developer tools, so you're not relying on a random weekend project. If you generate IDs in JavaScript, this should probably be your default.

Back to Projects
Project ID: 1b40dd07-4a64-4f21-be9b-b1647f4d1510Last updated: September 30, 2026 at 02:53 AM