DEV Community

MeloAlright
MeloAlright

Posted on

ColorID: 🌈 The unique 4-colors-ID string generator in JavaScript

Image description

Color as Identify - The unique 4-colors-ID string generator in JavaScript

The performance of ColorID is better than UUID and NanoID (The algorithm of ColorID is actually very similar to the UUID.V4)

If we use ColorID to represent identities in social networks, we can easily generate personalized social information for users, such as avatars, NFTs, etc.

The ColorID is consisted by 4 colors in the RGB channel from 12 unsigned 8 bits numbers (u8), so the theoretical total of ColorID is (2^8)^12 = 2^96 ≈ 7*10^28, which means that even if the ColorID is generated for every 7.8 billion people in the world every second, it will take 300 billion years to use up all ColorIDs. And ColorID provides safety by using hardware random generator, can also be used in clusters.

Because the Four color theorem, if we need to color the regions of any avatars or NFTs so that no two adjacent regions have the same color, four colors are enough!

Install

$ npm i colorid.js
Enter fullscreen mode Exit fullscreen mode

Usage

import colorid from "colorid.js";

colorid(); // #5B34F9-#34F9DF-#F9DF4E-#DF4EB5
Enter fullscreen mode Exit fullscreen mode

#5B34F9-#34F9DF-#F9DF4E-#DF4EB5

Performance

crypto.randomUUID    8,829,532 ops/sec
colorid              3,252,649 ops/sec
nanoid               2,925,204 ops/sec
uuid v4                910,855 ops/sec
Enter fullscreen mode Exit fullscreen mode

Benchmark configuration: Ubuntu, Intel(R) Xeon(R) Platinum 8171M CPU @ 2.60GHz, Node.js 18.9.0

(runs: 3045998209)

License

MIT

ColorID in Other Languages

Top comments (0)