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

Commit b57fc3f

Browse files
authored
Merge pull request #322 from ckeditor/i/6188
Other: The `uid()` helper should be a lot faster. Closes ckeditor/ckeditor5#6188.
2 parents e63b7b7 + 77b9d3c commit b57fc3f

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

src/uid.js

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,53 @@
77
* @module utils/uid
88
*/
99

10+
// A hash table of hex numbers to avoid using toString() in uid() which is costly.
11+
// [ '00', '01', '02', ..., 'fe', 'ff' ]
12+
const HEX_NUMBERS = new Array( 256 ).fill()
13+
.map( ( val, index ) => ( '0' + ( index ).toString( 16 ) ).slice( -2 ) );
14+
1015
/**
11-
* Returns a unique id. This id consist of an 'e' character and a randomly generated string of 32 aphanumeric characters.
12-
* Each character in uid string represents a hexadecimal digit (base 16).
16+
* Returns a unique id. The id starts with an "e" character and a randomly generated string of
17+
* 32 alphanumeric characters.
18+
*
19+
* **Note**: The characters the unique id is built from correspond to the hex number notation
20+
* (from "0" to "9", from "a" to "f"). In other words, each id corresponds to an "e" followed
21+
* by 16 8-bit numbers next to each other.
1322
*
14-
* @returns {String} A hexadecimal number representing the id.
23+
* @returns {String} An unique id string.
1524
*/
1625
export default function uid() {
17-
let uuid = 'e'; // Make sure that id does not start with number.
18-
19-
for ( let i = 0; i < 8; i++ ) {
20-
uuid += Math.floor( ( 1 + Math.random() ) * 0x10000 ).toString( 16 ).substring( 1 );
21-
}
26+
// Let's create some positive random 32bit integers first.
27+
//
28+
// 1. Math.random() is a float between 0 and 1.
29+
// 2. 0x100000000 is 2^32 = 4294967296.
30+
// 3. >>> 0 enforces integer (in JS all numbers are floating point).
31+
//
32+
// For instance:
33+
// Math.random() * 0x100000000 = 3366450031.853859
34+
// but
35+
// Math.random() * 0x100000000 >>> 0 = 3366450031.
36+
const r1 = Math.random() * 0x100000000 >>> 0;
37+
const r2 = Math.random() * 0x100000000 >>> 0;
38+
const r3 = Math.random() * 0x100000000 >>> 0;
39+
const r4 = Math.random() * 0x100000000 >>> 0;
2240

23-
return uuid;
41+
// Make sure that id does not start with number.
42+
return 'e' +
43+
HEX_NUMBERS[ r1 >> 0 & 0xFF ] +
44+
HEX_NUMBERS[ r1 >> 8 & 0xFF ] +
45+
HEX_NUMBERS[ r1 >> 16 & 0xFF ] +
46+
HEX_NUMBERS[ r1 >> 24 & 0xFF ] +
47+
HEX_NUMBERS[ r2 >> 0 & 0xFF ] +
48+
HEX_NUMBERS[ r2 >> 8 & 0xFF ] +
49+
HEX_NUMBERS[ r2 >> 16 & 0xFF ] +
50+
HEX_NUMBERS[ r2 >> 24 & 0xFF ] +
51+
HEX_NUMBERS[ r3 >> 0 & 0xFF ] +
52+
HEX_NUMBERS[ r3 >> 8 & 0xFF ] +
53+
HEX_NUMBERS[ r3 >> 16 & 0xFF ] +
54+
HEX_NUMBERS[ r3 >> 24 & 0xFF ] +
55+
HEX_NUMBERS[ r4 >> 0 & 0xFF ] +
56+
HEX_NUMBERS[ r4 >> 8 & 0xFF ] +
57+
HEX_NUMBERS[ r4 >> 16 & 0xFF ] +
58+
HEX_NUMBERS[ r4 >> 24 & 0xFF ];
2459
}

0 commit comments

Comments
 (0)