Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #322 from ckeditor/i/6188
Browse files Browse the repository at this point in the history
Other: The `uid()` helper should be a lot faster. Closes ckeditor/ckeditor5#6188.
  • Loading branch information
Reinmar authored Feb 4, 2020
2 parents e63b7b7 + 77b9d3c commit b57fc3f
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions src/uid.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,53 @@
* @module utils/uid
*/

// A hash table of hex numbers to avoid using toString() in uid() which is costly.
// [ '00', '01', '02', ..., 'fe', 'ff' ]
const HEX_NUMBERS = new Array( 256 ).fill()
.map( ( val, index ) => ( '0' + ( index ).toString( 16 ) ).slice( -2 ) );

/**
* Returns a unique id. This id consist of an 'e' character and a randomly generated string of 32 aphanumeric characters.
* Each character in uid string represents a hexadecimal digit (base 16).
* Returns a unique id. The id starts with an "e" character and a randomly generated string of
* 32 alphanumeric characters.
*
* **Note**: The characters the unique id is built from correspond to the hex number notation
* (from "0" to "9", from "a" to "f"). In other words, each id corresponds to an "e" followed
* by 16 8-bit numbers next to each other.
*
* @returns {String} A hexadecimal number representing the id.
* @returns {String} An unique id string.
*/
export default function uid() {
let uuid = 'e'; // Make sure that id does not start with number.

for ( let i = 0; i < 8; i++ ) {
uuid += Math.floor( ( 1 + Math.random() ) * 0x10000 ).toString( 16 ).substring( 1 );
}
// Let's create some positive random 32bit integers first.
//
// 1. Math.random() is a float between 0 and 1.
// 2. 0x100000000 is 2^32 = 4294967296.
// 3. >>> 0 enforces integer (in JS all numbers are floating point).
//
// For instance:
// Math.random() * 0x100000000 = 3366450031.853859
// but
// Math.random() * 0x100000000 >>> 0 = 3366450031.
const r1 = Math.random() * 0x100000000 >>> 0;
const r2 = Math.random() * 0x100000000 >>> 0;
const r3 = Math.random() * 0x100000000 >>> 0;
const r4 = Math.random() * 0x100000000 >>> 0;

return uuid;
// Make sure that id does not start with number.
return 'e' +
HEX_NUMBERS[ r1 >> 0 & 0xFF ] +
HEX_NUMBERS[ r1 >> 8 & 0xFF ] +
HEX_NUMBERS[ r1 >> 16 & 0xFF ] +
HEX_NUMBERS[ r1 >> 24 & 0xFF ] +
HEX_NUMBERS[ r2 >> 0 & 0xFF ] +
HEX_NUMBERS[ r2 >> 8 & 0xFF ] +
HEX_NUMBERS[ r2 >> 16 & 0xFF ] +
HEX_NUMBERS[ r2 >> 24 & 0xFF ] +
HEX_NUMBERS[ r3 >> 0 & 0xFF ] +
HEX_NUMBERS[ r3 >> 8 & 0xFF ] +
HEX_NUMBERS[ r3 >> 16 & 0xFF ] +
HEX_NUMBERS[ r3 >> 24 & 0xFF ] +
HEX_NUMBERS[ r4 >> 0 & 0xFF ] +
HEX_NUMBERS[ r4 >> 8 & 0xFF ] +
HEX_NUMBERS[ r4 >> 16 & 0xFF ] +
HEX_NUMBERS[ r4 >> 24 & 0xFF ];
}

0 comments on commit b57fc3f

Please sign in to comment.