You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nano ID is quite comparable to UUID v4 (random-based).
It has a similar number of random bits in the ID
(126 in Nano ID and 122 in UUID), so it has a similar collision probability:
For there to be a one in a billion chance of duplication,
103 trillion version 4 IDs must be generated.
There are three main differences between Nano ID and UUID v4:
Nano ID uses a bigger alphabet, so a similar number of random bits
are packed in just 21 symbols instead of 36.
Nano ID code is 4.5 times less than uuid/v4 package:
108 bytes instead of 483.
Because of memory allocation tricks, Nano ID is 60% faster than UUID.
Unpredictability. Instead of using the unsafe Math.random(), Nano ID
uses the crypto module in Node.js and the Web Crypto API in browsers.
These modules use unpredictable hardware random generator.
Uniformity.random % alphabet is a popular mistake to make when coding
an ID generator. The distribution will not be even; there will be a lower
chance for some symbols to appear compared to others. So, it will reduce
the number of tries when brute-forcing. Nano ID uses a better algorithm
and is tested for uniformity.
Vulnerabilities: to report a security vulnerability, please use
the Tidelift security contact.
Tidelift will coordinate the fix and disclosure.
Usage
JS
The main module uses URL-friendly symbols (A-Za-z0-9_-) and returns an ID
with 21 characters (to have a collision probability similar to UUID v4).
In case you don’t have stable ids you'd rather use index as key
instead of nanoid():
consttodoItems=todos.map((text,index)=><likey={index}> /* Still not recommended but preferred over nanoid().
Only do this if items have no stable IDs. */
{text}</li>)
If you want to use Nano ID in the id prop, you must set some string prefix
(it is invalid for the HTML ID to start with a number).
<inputid={'id'+this.id}type="text"/>
Create React App
Create React App < 4.0.0 had a problem with ES modules packages.
TypeError: (0 , _nanoid.nanoid) is not a function
Use Nano ID 2 npm i nanoid@^2.0.0 if you're using a version below
CRA 4.0.
React Native
React Native does not have built-in random generator. The following polyfill
works for plain React Native and Expo starting with 39.x.
In PouchDB and CouchDB, IDs can’t start with an underscore _.
A prefix is required to prevent this issue, as Nano ID might use a _
at the start of the ID by default.
Override the default ID with the following option:
Web Workers do not have access to a secure random generator.
Security is important in IDs when IDs should be unpredictable.
For instance, in "access by URL" link generation.
If you do not need unpredictable IDs, but you need to use Web Workers,
you can use the non‑secure ID generator.
Also, CLI is available to generate IDs from a command line.
API
Async
To generate hardware random bytes, CPU collects electromagnetic noise.
In the synchronous API during the noise collection, the CPU is busy and
cannot do anything useful in parallel.
Using the asynchronous API of Nano ID, another code can run during
the entropy collection.
Unfortunately, you will lose Web Crypto API advantages in a browser
if you use the asynchronous API. So, currently, in the browser, you are limited
with either security or asynchronous behavior.
Non-Secure
By default, Nano ID uses hardware random bytes generation for security
and low collision probability. If you are not so concerned with security
and more concerned with performance, you can use the faster non-secure generator.
https://github.com/ai/nanoid/
Nano ID
A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
Size Limit controls the size.
Can be used in clusters.
A-Za-z0-9_-
).So ID size was reduced from 36 to 21 symbols.
to 14 programming languages.
Supports modern browsers, IE with Babel, Node.js and React Native.
Table of Contents
Comparison with UUID
Nano ID is quite comparable to UUID v4 (random-based).
It has a similar number of random bits in the ID
(126 in Nano ID and 122 in UUID), so it has a similar collision probability:
There are three main differences between Nano ID and UUID v4:
are packed in just 21 symbols instead of 36.
uuid/v4
package:108 bytes instead of 483.
Benchmark
Test configuration: Dell XPS 2-in-1 7390, Fedora 32, Node.js 15.1.
Tools
the ID alphabet or size.
nanoid-dictionary
with popular alphabets to use withcustomAlphabet
.nanoid-good
to be sure that your ID doesn’t contain any obscene words.Security
See a good article about random generators theory:
Secure random values (in Node.js)
Unpredictability. Instead of using the unsafe
Math.random()
, Nano IDuses the
crypto
module in Node.js and the Web Crypto API in browsers.These modules use unpredictable hardware random generator.
Uniformity.
random % alphabet
is a popular mistake to make when codingan ID generator. The distribution will not be even; there will be a lower
chance for some symbols to appear compared to others. So, it will reduce
the number of tries when brute-forcing. Nano ID uses a better algorithm
and is tested for uniformity.
Vulnerabilities: to report a security vulnerability, please use
the Tidelift security contact.
Tidelift will coordinate the fix and disclosure.
Usage
JS
The main module uses URL-friendly symbols (
A-Za-z0-9_-
) and returns an IDwith 21 characters (to have a collision probability similar to UUID v4).
In Node.js you can use CommonJS import:
If you want to reduce the ID size (and increase collisions probability),
you can pass the size as an argument.
Don’t forget to check the safety of your ID size
in our ID collision probability calculator.
You can also use a custom alphabet
or a random generator.
IE
If you support IE, you need to transpile
node_modules
by Babeland add
crypto
alias:React
There’s currently no correct way to use nanoid for React
key
propsince it should be consistent among renders.
You should rather try to reach for stable id inside your list item.
In case you don’t have stable ids you'd rather use index as
key
instead of
nanoid()
:If you want to use Nano ID in the
id
prop, you must set some string prefix(it is invalid for the HTML ID to start with a number).
Create React App
Create React App < 4.0.0 had
a problem with ES modules packages.
Use Nano ID 2
npm i nanoid@^2.0.0
if you're using a version belowCRA 4.0.
React Native
React Native does not have built-in random generator. The following polyfill
works for plain React Native and Expo starting with
39.x
.react-native-get-random-values
docs and install it.For Expo framework see the next section.
Rollup
For Rollup you will need
@rollup/plugin-node-resolve
to bundle browser versionof this library and
@rollup/plugin-replace
to replaceprocess.env.NODE_ENV
:PouchDB and CouchDB
In PouchDB and CouchDB, IDs can’t start with an underscore
_
.A prefix is required to prevent this issue, as Nano ID might use a
_
at the start of the ID by default.
Override the default ID with the following option:
Mongoose
ES Modules
Nano ID provides ES modules. You do not need to do anything to use Nano ID
as ESM in webpack, Rollup, Parcel, or Node.js.
For quick hacks, you can load Nano ID from CDN. Special minified
nanoid.js
module is available on jsDelivr.Though, it is not recommended to be used in production
because of the lower loading performance.
Web Workers
Web Workers do not have access to a secure random generator.
Security is important in IDs when IDs should be unpredictable.
For instance, in "access by URL" link generation.
If you do not need unpredictable IDs, but you need to use Web Workers,
you can use the non‑secure ID generator.
Note: non-secure IDs are more prone to collision attacks.
CLI
You can get unique ID in terminal by calling
npx nanoid
. You need onlyNode.js in the system. You do not need Nano ID to be installed anywhere.
$ npx nanoid npx: installed 1 in 0.63s LZfXLFzPPR4NNrgjlWDxn
If you want to change alphabet or ID size, you should use
nanoid-cli
.Other Programming Languages
Nano ID was ported to many languages. You can use these ports to have
the same ID generator on the client and server side.
with dictionaries
Also, CLI is available to generate IDs from a command line.
API
Async
To generate hardware random bytes, CPU collects electromagnetic noise.
In the synchronous API during the noise collection, the CPU is busy and
cannot do anything useful in parallel.
Using the asynchronous API of Nano ID, another code can run during
the entropy collection.
Unfortunately, you will lose Web Crypto API advantages in a browser
if you use the asynchronous API. So, currently, in the browser, you are limited
with either security or asynchronous behavior.
Non-Secure
By default, Nano ID uses hardware random bytes generation for security
and low collision probability. If you are not so concerned with security
and more concerned with performance, you can use the faster non-secure generator.
Note: your IDs will be more predictable and prone to collision attacks.
Custom Alphabet or Size
customAlphabet
allows you to createnanoid
with your own alphabetand ID size.
Check the safety of your custom alphabet and ID size in our
ID collision probability calculator. For more alphabets, check out the options
in
nanoid-dictionary
.Alphabet must contain 256 symbols or less.
Otherwise, the security of the internal generator algorithm is not guaranteed.
Customizable asynchronous and non-secure APIs are also available:
Custom Random Bytes Generator
customRandom
allows you to create ananoid
and replace alphabetand the default random bytes generator.
In this example, a seed-based generator is used:
random
callback must accept the array size and return an arraywith random numbers.
If you want to use the same URL-friendly symbols with
customRandom
,you can get the default alphabet using the
urlAlphabet
.Asynchronous and non-secure APIs are not available for
customRandom
.The text was updated successfully, but these errors were encountered: