Skip to content

Commit

Permalink
Reduce length of generateStrongCompactUuid
Browse files Browse the repository at this point in the history
  • Loading branch information
Desno365 committed Apr 9, 2022
1 parent 46a71af commit 79e85b7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ES6 module syntax:
import { generateCustomUuid, generateShortUuid, generateStrongCompactUuid } from "custom-uuid";
generateCustomUuid("123456789ABC", 20); // ⇨ 'C12B1B2A9382A488B43A'
generateShortUuid(); // ⇨ 'yT1xoeCt6fvdDf6a'
generateStrongCompactUuid(); // ⇨ 'BYFGhRjnn83hHCarT09HKJ'
generateStrongCompactUuid(); // ⇨ 'BYFGhRjnn83hHCarT09H'
```

CommonJS syntax:
Expand All @@ -49,7 +49,7 @@ CommonJS syntax:
const { generateCustomUuid, generateShortUuid, generateStrongCompactUuid } = require('custom-uuid');
generateCustomUuid("123456789ABC", 20); // ⇨ 'B5B6699247862A569998'
generateShortUuid(); // ⇨ 'DMDvkPec8QUyV9O1'
generateStrongCompactUuid(); // ⇨ 'xRC4JggRQQFdPwn6MhZs08'
generateStrongCompactUuid(); // ⇨ 'xRC4JggRQQFdPwn6MhZs'
```

#### 3. Make sure the custom UUID is unique enough for your use case
Expand All @@ -69,7 +69,7 @@ Or you can use one of the pre-defined UUIDs that have strong guarantees of uniqu
| Method | Length | Example output |
|-----------------------------------------------------------------------------------------------|--------|----------------------------------------|
| [`generateShortUuid()`](#generateShortUuid) | 16 | `14usBY8xSYXGPvsA` |
| [`generateStrongCompactUuid()`](#generateStrongCompactUuid) | 22 | `6ptGBhTKkxTMCMEiiHiwwj` |
| [`generateStrongCompactUuid()`](#generateStrongCompactUuid) | 20 | `6ptGBhTKkxTMCMEiiHiw` |
| [`generateShortLowercaseUuid()`](#generateShortLowercaseUuid) | 20 | `15amp61jbnu6dzmhxa0i` |
| [`generateLongLowercaseUuid()`](#generateLongLowercaseUuidshouldRemoveHyphens-boolean--false) | 36 | `e3703960-ca2d-4802-b426-88467e0e9b98` |
| [`generateProfanitySafeUuid()`](#generateProfanitySafeUuid) | 20 | `4a8g6z1w7d1a8d1o9o3o` |
Expand All @@ -89,10 +89,10 @@ Use this identifier when you need a strong but very short universally unique ide
Generates a short cryptographically-strong random UUID using numbers and letters.\
Use this identifier when you need a very strong but still compact universally unique id.

* **Length:** 22
* **Length:** 20
* **Dictionary:** `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`
* **Example output:** `6ptGBhTKkxTMCMEiiHiwwj`
* **Average UUIDs to be generated before having the first collision:** 6.52e+19
* **Example output:** `6ptGBhTKkxTMCMEiiHiw`
* **Average UUIDs to be generated before having the first collision:** 1.05e+18

#### generateShortLowercaseUuid()

Expand Down
12 changes: 6 additions & 6 deletions src/uuid-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ export function generateShortUuid(): string {

/**
* Generates a short cryptographically-strong random UUID using numbers and letters.
* Example: "6ptGBhTKkxTMCMEiiHiwwj".
* Example: "6ptGBhTKkxTMCMEiiHiw".
* Use this identifier when you need a very strong but still compact universally unique id.
*
* Length of this UUID: 22 characters.
* Total number of possible UUIDs: 62^22 = 2.70e+39, precisely 2'707'803'647'802'660'400'290'261'537'185'326'956'544
* Probability of creating a duplicate ID when creating one billion (10^9, giga-unit) UUIDs: k^2÷2N = ((10^9)^2)÷(2*(62^22)) = 0.0000000000000000000185% (https://preshing.com/20110504/hash-collision-probabilities/)
* Average UUIDs to be generated before having the first collision: sqrt(pi*0.5*62^22) = 6.52e+19 (https://shortunique.id/classes/default.html#approxmaxbeforecollision)
* Length of this UUID: 20 characters.
* Total number of possible UUIDs: 62^20 = 7.04e+35, precisely 704'423'425'546'998'022'968'330'264'616'370'176
* Probability of creating a duplicate ID when creating one billion (10^9, giga-unit) UUIDs: k^2÷2N = ((10^9)^2)÷(2*(62^20)) = 0.000000000000000071% (https://preshing.com/20110504/hash-collision-probabilities/)
* Average UUIDs to be generated before having the first collision: sqrt(pi*0.5*62^20) = 1.05e+18 (https://shortunique.id/classes/default.html#approxmaxbeforecollision)
*/
export function generateStrongCompactUuid(): string {
return generateCustomUuid("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 22);
return generateCustomUuid("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 20);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/uuid-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('uuidGenerator', function() {
for (let i = 0; i < DEFAULT_NUMBER_OF_TEST_SAMPLES; i++) {
const uuid = uuidGenerator.generateStrongCompactUuid();
expect(uuid).to.be.a("string");
assert.equal(uuid.length, 22);
assert.equal(uuid.length, 20);
expect(doesStringContainOnlySimpleCharacters(uuid)).to.be.true;
listOfUuids.push(uuid);
}
Expand Down

0 comments on commit 79e85b7

Please sign in to comment.