What is scuid?
The scuid npm package is designed to generate short, collision-resistant, URL-friendly unique identifiers. These identifiers are suitable for use where a unique string is necessary, such as database keys, component ids, or any other unique element identifier in a distributed system.
What are scuid's main functionalities?
Generating a unique identifier
This feature allows the generation of a unique identifier string that is URL-friendly and highly unlikely to collide with other generated identifiers.
"const scuid = require('scuid');
const id = scuid();
console.log(id); // Outputs a unique identifier string"
Generating a unique identifier with a custom prefix
This feature allows the generation of a unique identifier with a custom prefix, which can be useful for namespacing or categorizing identifiers.
"const scuid = require('scuid');
const idWithPrefix = scuid('usr_');
console.log(idWithPrefix); // Outputs a unique identifier string with 'usr_' prefix"
Other packages similar to scuid
uuid
The uuid package generates RFC-compliant UUIDs. It offers different versions of UUIDs, such as v1 (timestamp-based), v4 (random), and others. Unlike scuid, which generates shorter, URL-friendly IDs, UUIDs are longer and standardized.
nanoid
NanoID is a tiny, secure, URL-friendly, unique string ID generator. It is similar to scuid in that it produces short, unique identifiers, but it also allows for custom alphabet and size, providing more flexibility in the generation process.
shortid
ShortId is another package for generating short non-sequential url-friendly unique ids. However, it has been deprecated in favor of NanoID due to the potential for local collisions under heavy use and other issues.
scuid
Collision-resistant IDs optimized for horizontal scaling and performance.
A slim, alternative, and compatible implementation of cuid for node,
also featuring a wide range of options, as well as custom random number generator support.
It can serve as a drop-in replacement, and is also faster than cuid.
Install via npm
$ npm install --save scuid
Usage
var scuid = require( 'scuid' )
Generate an ID
var id = scuid()
> 'ciux3hs0x0000io10cusdm8r2'
Generate a slug
var slug = scuid.slug()
> '6x1i0r0'
Get the process' fingerprint
var fingerprint = scuid.fingerprint()
> 'io10'
Use a custom (P)RNG
var generator = {
random: function() {
return 5
}
}
var scuid = require( 'scuid' ).create({
rng: generator
})
Use other custom options
Note that fiddeling with these might make your IDs incompatible with cuid's guarantees.
var scuid = require( 'scuid' ).create({
prefix: 'c',
base: 36,
blockSize: 4,
fill: '0',
pid: process.pid,
fingerprint: scuid.createFingerprint( [pid], [hostname] ),
rng: Math,
})
Tests
Just like cuid
, collision resistance for both – slugs and IDs – is tested
over 1 million and 2 million iterations respectively.
To run the tests, run:
$ npm test
Benchmarks
$ npm run benchmark
scuid
id ............................................. 573,618 op/s
slug ........................................... 673,732 op/s
fingerprint .................................... 131,156,394 op/s
cuid
id ............................................. 445,260 op/s
slug ........................................... 531,770 op/s
fingerprint .................................... 125,159,685 op/s