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