What is @nats-io/nuid?
@nats-io/nuid is a simple and fast library for generating unique identifiers. It is particularly useful in distributed systems where unique identifiers are needed for messages or entities.
What are @nats-io/nuid's main functionalities?
Generate Unique Identifiers
This feature allows you to generate a unique identifier using the Nuid class. The `next()` method generates a new unique ID each time it is called. This is useful for creating unique identifiers for messages or entities in distributed systems.
const { Nuid } = require('@nats-io/nuid');
const nuid = new Nuid();
const uniqueId = nuid.next();
console.log(uniqueId);
Reset Nuid
The `reset()` method allows you to reset the internal state of the Nuid instance, which can be useful if you want to start generating a new sequence of unique identifiers.
const { Nuid } = require('@nats-io/nuid');
const nuid = new Nuid();
nuid.reset();
const newUniqueId = nuid.next();
console.log(newUniqueId);
Other packages similar to @nats-io/nuid
uuid
The 'uuid' package is a popular library for generating UUIDs (Universally Unique Identifiers). It provides different versions of UUIDs, such as v1, v3, v4, and v5. Compared to @nats-io/nuid, 'uuid' is more focused on generating standard UUIDs, which are widely used and recognized across different systems.
shortid
The 'shortid' package is used for generating short, non-sequential, URL-friendly unique IDs. It is similar to @nats-io/nuid in that it provides unique identifiers, but 'shortid' focuses on creating shorter IDs that are easier to use in URLs and other contexts where brevity is important.
nanoid
The 'nanoid' package is a modern, fast, and secure unique string ID generator. It is similar to @nats-io/nuid in terms of generating unique identifiers, but 'nanoid' emphasizes security and performance, making it suitable for use in environments where these factors are critical.
Nuid.js



A highly performant unique identifier generator for JavaScript.
Installation
For web and deno you can use the JSR bundle:
npx jsr add @nats-io/nuid
// or
deno add @nats-io/nuid
import { next, Nuid } from "jsr:@nats-io/nuid";
In node/bun:
npm install nuid
const { next, Nuid } = require("nuid");
import { next, Nuid } from "nuid";
Basic Usage
let id = nuid.next();
id = nuid.next();
nuid.reset();
id = nuid.next();
Performance
NUID needs to be very fast to generate and be truly unique, all while being
entropy pool friendly. NUID uses 12 bytes of crypto generated data (entropy
draining), and 10 bytes of pseudo-random sequential data that increments with a
pseudo-random increment.
Total length of a NUID string is 22 bytes of base 36 ascii text, so 36^22 or
17324272922341479351919144385642496 possibilities.
Migration
The 2.x version of the npm module support both CJS and ESM modules, an ESM only
version of the module is available via
jsr @nats-io/nuid
If you are migrating from the 1.x.x series, note that getGlobalNuid()
,
next()
and reset()
and version
property have been removed. Instead, access
the exported constant nuid
and call next()
or reset()
on it as shown in
the examples above. For version information please refer to your installed
module's version information.
If you are migrating from the js-nuid
module in npm, there should be no
changes except to the location of the import in the npm bundle:
import { nuid } from "./node_modules/esm/index.js";
Supported Node Versions
Support policy for Nodejs versions follows
Nodejs release support. We will support and
build nuid on even Nodejs versions that are current or in maintenance.
License
Unless otherwise noted, the NATS source files are distributed under the Apache
Version 2.0 license found in the LICENSE file.