This is the 22nd day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021


This article is from: medium.com/abdelmatyn…

Universally Unique Identifier (UUID) is a 128-bit Identifier used in computer systems to identify information, usually represented as a string of 32-bit hexadecimal numbers.

UUID is used to solve the problem of unique ID!

Then, how to ensure uniqueness is a challenge in itself!

How do I ensure that only one copy of the ID is generated? How do I guarantee that there is no correlation between two ids? How to choose between uniqueness and randomness……

(OS: Bengwa’s previous book, understanding P/NP Problems, gave me the illusion that I had hit the ceiling of human cognition?! Friends of this article should know: maybe there is no such thing as randomness? Any randomness can be worked out by quantum computations. Does God roll dice? No one knows……)

If there is real randomness, press no table first,

Based on the accuracy of the current calculation force, various UUID generators and different versions of the current processing methods can ensure that the ID does not repeat, the probability of duplicate UUID code is close to zero, can be ignored.

This article brings you three UUID generators! 👍 👍 👍

UUID

UUID based on RFC4122 standard, it has many versions: v1,v2.. V5.

Uuid V1 is generated using a combination of the host MAC address and the current date and time, which means that the UUID is anonymous.

And another line from exclusions is that uUID V4 is generated randomly, and there is no inherent logic, and there are many different types of medlines. unless trillions of ids are generated every second, there is almost no duplication.

Uuid V5, unlike V1 V4, is generated by providing two inputs (an input string and a namespace) that are converted to uUID;

Features:

  • Perfect;
  • Cross-platform;
  • Security: encryption, strong randomness;
  • Small size: zero dependence, small space occupation;
  • Good open source library support: UUID Command line;

Get started:

import { v4 as uuidv4 } from 'uuid'; let uuid = uuidv4(); The console. The log (uuid) / / ⇨ 'b7d b1deb4d - 3-4 bad - 9 of 9 BDD - 2 b0d7b3dcb6d'Copy the code

Crypto.randomUUID

Node.js API Crypto provides **randomUUID()** method, based on RFC 4122 V4 generate random number;

Get started:

let uuid = crypto.randomUUID(); console.log(uuid); / / ⇨ "36 b8f84d - d49 df4e - 4 - b662 - bcde71a8764f"Copy the code

Nano ID

Nano ID has 3 apis:

  1. Normal (blocking); ordinary
  2. Asynchronous; asynchronous
  3. Non – secure; The security

By default, Nano ID uses the symbol (A-zA-Z0-9 -) and returns an ID of 21 characters (with A repetition probability similar to UUID V4).

Features:

  • Small size: 130 bytes (after compression);
  • Zero rely on;
  • Faster generation;
  • Security:
  • Shorter, just 21 bits;
  • Easy portability, support for 20 programming languages.

Get started:

import { nanoid } from 'nanoid' let uuid = nanoid(); The console. The log (uuid) / / ⇨ V1StGXR8_Z5jdHi6B myT - ""Copy the code

Nano IDnpm download trend:

speed

Let’s compare the speed differences of the three UUID generation methods mentioned above:

// test-uuid-gen.js
const { v4 as uuidv4 } = require('uuid');

for (let i = 0; i < 10_000_000; i++) {
  uuidv4();
}

// test-crypto-gen.js
const { randomUUID } = require('crypto');

for (let i = 0; i < 10_000_000; i++) {
  randomUUID();
}

// test-nanoid-gen.js
const { nanoid } = require('nanoid');

for (let i = 0; i < 10_000_000; i++) {
  nanoid();
}
Copy the code

With the help of hyperfine;

Hyperfine ‘node test-uid-gen.js” node test-crypto-gen.js’ ‘node test-nanoid-gen.js’

Running results:

As you can see, the second randomUUID() is about 4 times faster than the third nanoid and about 12 times faster than the first uUID


OK, the above is the share ~ writing is not easy, like encourage 👍👍👍

I am Anthony Nuggets, the public account of the same name, every day a pawn, dig a gold, goodbye ~