npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@osjwnpm/placeat-nulla-deserunt

v1.0.0

Published

<img src="https://ai.github.io/@osjwnpm/placeat-nulla-deserunt/logo.svg" align="right" alt="Nano ID logo by Anton Lovchikov" width="180" height="94">

Downloads

4

Maintainers

lequoc12780lequoc12780

Keywords

parsestyled-componentsbrowserslistRFC-6455arraybytel10neveryoncefseventsmonorepodotenvfindLastIndeximmutableassertnegative zeroform-validationFunction.prototype.nameIteratorinstallermiddlewareprettyES2018collectionhttpfile systemdomfunctionalworkerfixed-widthlintoptioninStreamspositiveoptimizerwatchingES2017computed-typesnodejsfiglethelperssqs@@toStringTagprogressslotpropertiesnamestssetterregular expressionmobileargumentlockfileJSON-Schemahasweakmapdeep-clonepushclassesStreamwriteECMAScript 2019copyless compilerjavascriptassertionloglistenersArray.prototype.filterWebSocketECMAScript 2016less cssdataviewAsyncIteratorkarmaRxJSpluginmkdirpinputlastES2022ArrayBufferartinstallownArray.prototype.flattenqueueMicrotaskforEachefficientroutecliprotoArray.prototype.flatlookapolloecmascripttypedarraysreadjsxTypedArrayelbutil.inspectexecajvStyleSheetbufferstablefast-copyuuidwalklesscsscss nestingequaldebugmodulerequestvalidateregularairbnbmomenteventslazyjsonArraycacheauththrottlefunctionsconcatscheme-validationpackageiamsafeflat_.extendrecursiveformfilestyletoSortedtoolsstdlibbundlershellES2019Array.prototype.containsObject.definePropertytermECMAScript 2018mimees2018valuesObject.entriesUint16ArrayfunctionTypeBox__proto__escapecharacterscloudsearchcomparetraverseInt8Arrayerrorless.jsreducersimpledbrfc4122consolemapfastcopyprivatepolyfillsymlinksObject.valueswritablesideutilArrayBuffer#slicehasOwnwordwrapzerobusyrm -frspeedieECMAScript 5jQueryBigUint64ArrayquotepreprocessorgenericstypeproxychannelmatchAllgettertest

Readme

Nano ID

English | Русский | 简体中文 | Bahasa Indonesia

A tiny, secure, URL-friendly, unique string ID generator for JavaScript.

“An amazing level of senseless perfectionism, which is simply impossible not to respect.”

  • Small. 116 bytes (minified and brotlied). No dependencies. Size Limit controls the size.
  • Safe. It uses hardware random generator. Can be used in clusters.
  • Short IDs. It uses a larger alphabet than UUID (A-Za-z0-9_-). So ID size was reduced from 36 to 21 symbols.
  • Portable. Nano ID was ported to over 20 programming languages.
import { @osjwnpm/placeat-nulla-deserunt } from '@osjwnpm/placeat-nulla-deserunt'
model.id = @osjwnpm/placeat-nulla-deserunt() //=> "V1StGXR8_Z5jdHi6B-myT"

  Made in Evil Martians, product consulting for developer tools.


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:

For there to be a one in a billion chance of duplication, 103 trillion version 4 IDs must be generated.

There are two main differences between Nano ID and UUID v4:

  1. Nano ID uses a bigger alphabet, so a similar number of random bits are packed in just 21 symbols instead of 36.
  2. Nano ID code is 4 times smaller than uuid/v4 package: 130 bytes instead of 423.

Benchmark

$ node ./test/benchmark.js
crypto.randomUUID         28,398,328 ops/sec
uuid v4                   10,254,199 ops/sec
@napi-rs/uuid             15,110,625 ops/sec
uid/secure                10,388,842 ops/sec
@lukeed/uuid               8,914,507 ops/sec
@osjwnpm/placeat-nulla-deserunt                     7,484,029 ops/sec
customAlphabet             4,867,017 ops/sec
@osjwnpm/placeat-nulla-deserunt for browser           609,426 ops/sec
secure-random-string         539,080 ops/sec
uid-safe.sync                533,944 ops/sec
shortid                       59,609 ops/sec

Non-secure:
uid                       95,579,977 ops/sec
@osjwnpm/placeat-nulla-deserunt/non-secure          3,999,766 ops/sec
rndm                       3,981,914 ops/sec

Test configuration: Framework 13 7840U, Fedora 39, Node.js 21.6.

Security

See a good article about random generators theory: Secure random values (in Node.js)

  • 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.

  • Well-documented: all Nano ID hacks are documented. See comments in the source.

  • Vulnerabilities: to report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

Install

npm install @osjwnpm/placeat-nulla-deserunt

Nano ID 5 works only with ESM projects, in tests or Node.js scripts. For CommonJS you need to use Node.js 22 with --experimental-require-module:

node --experimental-require-module app.js

Or you can use Nano ID 3.x (we still support it):

npm install @osjwnpm/placeat-nulla-deserunt@3

For quick hacks, you can load Nano ID from CDN. Though, it is not recommended to be used in production because of the lower loading performance.

import { @osjwnpm/placeat-nulla-deserunt } from 'https://cdn.jsdelivr.net/npm/@osjwnpm/placeat-nulla-deserunt/@osjwnpm/placeat-nulla-deserunt.js'

API

Nano ID has 2 APIs: normal and non-secure.

By default, Nano ID uses URL-friendly symbols (A-Za-z0-9_-) and returns an ID with 21 characters (to have a collision probability similar to UUID v4).

Blocking

The safe and easiest way to use Nano ID.

In rare cases could block CPU from other work while noise collection for hardware random generator.

import { @osjwnpm/placeat-nulla-deserunt } from '@osjwnpm/placeat-nulla-deserunt'
model.id = @osjwnpm/placeat-nulla-deserunt() //=> "V1StGXR8_Z5jdHi6B-myT"

If you want to reduce the ID size (and increase collisions probability), you can pass the size as an argument.

@osjwnpm/placeat-nulla-deserunt(10) //=> "IRFa-VaY2b"

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.

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, you can use it for environments without hardware random generators.

import { @osjwnpm/placeat-nulla-deserunt } from '@osjwnpm/placeat-nulla-deserunt/non-secure'
const id = @osjwnpm/placeat-nulla-deserunt() //=> "Uakgb_J5m9g-0JDMbcJqLJ"

Custom Alphabet or Size

customAlphabet returns a function that allows you to create @osjwnpm/placeat-nulla-deserunt with your own alphabet and ID size.

import { customAlphabet } from '@osjwnpm/placeat-nulla-deserunt'
const @osjwnpm/placeat-nulla-deserunt = customAlphabet('1234567890abcdef', 10)
model.id = @osjwnpm/placeat-nulla-deserunt() //=> "4f90d13a42"
import { customAlphabet } from '@osjwnpm/placeat-nulla-deserunt/non-secure'
const @osjwnpm/placeat-nulla-deserunt = customAlphabet('1234567890abcdef', 10)
user.id = @osjwnpm/placeat-nulla-deserunt()

Check the safety of your custom alphabet and ID size in our ID collision probability calculator. For more alphabets, check out the options in @osjwnpm/placeat-nulla-deserunt-dictionary.

Alphabet must contain 256 symbols or less. Otherwise, the security of the internal generator algorithm is not guaranteed.

In addition to setting a default size, you can change the ID size when calling the function:

import { customAlphabet } from '@osjwnpm/placeat-nulla-deserunt'
const @osjwnpm/placeat-nulla-deserunt = customAlphabet('1234567890abcdef', 10)
model.id = @osjwnpm/placeat-nulla-deserunt(5) //=> "f01a2"

Custom Random Bytes Generator

customRandom allows you to create a @osjwnpm/placeat-nulla-deserunt and replace alphabet and the default random bytes generator.

In this example, a seed-based generator is used:

import { customRandom } from '@osjwnpm/placeat-nulla-deserunt'

const rng = seedrandom(seed)
const @osjwnpm/placeat-nulla-deserunt = customRandom('abcdef', 10, size => {
  return (new Uint8Array(size)).map(() => 256 * rng())
})

@osjwnpm/placeat-nulla-deserunt() //=> "fbaefaadeb"

random callback must accept the array size and return an array with random numbers.

If you want to use the same URL-friendly symbols with customRandom, you can get the default alphabet using the urlAlphabet.

const { customRandom, urlAlphabet } = require('@osjwnpm/placeat-nulla-deserunt')
const @osjwnpm/placeat-nulla-deserunt = customRandom(urlAlphabet, 10, random)

Note, that between Nano ID versions we may change random generator call sequence. If you are using seed-based generators, we do not guarantee the same result.

Usage

React

There’s no correct way to use Nano ID for React key prop since it should be consistent among renders.

function Todos({todos}) {
  return (
    <ul>
      {todos.map(todo => (
        <li key={@osjwnpm/placeat-nulla-deserunt()}> /* DON’T DO IT */
          {todo.text}
        </li>
      ))}
    </ul>
  )
}

You should rather try to reach for stable ID inside your list item.

const todoItems = todos.map((todo) =>
  <li key={todo.id}>
    {todo.text}
  </li>
)

In case you don’t have stable IDs you'd rather use index as key instead of @osjwnpm/placeat-nulla-deserunt():

const todoItems = todos.map((text, index) =>
  <li key={index}> /* Still not recommended but preferred over @osjwnpm/placeat-nulla-deserunt().
                      Only do this if items have no stable IDs. */
    {text}
  </li>
)

In case you just need random IDs to link elements like labels and input fields together, useId is recommended. That hook was added in React 18.

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.

  1. Check react-native-get-random-values docs and install it.
  2. Import it before Nano ID.
import 'react-native-get-random-values'
import { @osjwnpm/placeat-nulla-deserunt } from '@osjwnpm/placeat-nulla-deserunt'

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:

db.put({
  _id: 'id' + @osjwnpm/placeat-nulla-deserunt(),
  …
})

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.

import { @osjwnpm/placeat-nulla-deserunt } from '@osjwnpm/placeat-nulla-deserunt/non-secure'
@osjwnpm/placeat-nulla-deserunt() //=> "Uakgb_J5m9g-0JDMbcJqLJ"

Note: non-secure IDs are more prone to collision attacks.

CLI

You can get unique ID in terminal by calling npx @osjwnpm/placeat-nulla-deserunt. You need only Node.js in the system. You do not need Nano ID to be installed anywhere.

$ npx @osjwnpm/placeat-nulla-deserunt
npx: installed 1 in 0.63s
LZfXLFzPPR4NNrgjlWDxn

Size of generated ID can be specified with --size (or -s) option:

$ npx @osjwnpm/placeat-nulla-deserunt --size 10
L3til0JS4z

Custom alphabet can be specified with --alphabet (or -a) option (note that in this case --size is required):

$ npx @osjwnpm/placeat-nulla-deserunt --alphabet abc --size 15
bccbcabaabaccab

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.

For other environments, CLI is available to generate IDs from a command line.

Tools