Fastest GUID like string generator method in the format XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
. This does not generate standard-compliant GUID.
Ten million executions of this implementation take just 32.5 seconds, which is the fastest I've ever seen in a browser (the only solution without loops/iterations).
The function is as simple as:
/**
* Generates a GUID string.
* @returns {string} The generated GUID.
* @example af8a8416-6e18-a307-bd9c-f2c947bbb3aa
* @author Slavik Meltser.
* @link http://slavik.meltser.info/?p=142
*/
function guid() {
function _p8(s) {
var p = (Math.random().toString(16)+"000000000").substr(2,8);
return s ? "-" + p.substr(0,4) + "-" + p.substr(4,4) : p ;
}
return _p8() + _p8(true) + _p8(true) + _p8();
}
To test the performance, you can run this code:
console.time('t');
for (var i = 0; i < 10000000; i++) {
guid();
};
console.timeEnd('t');
I'm sure most of you will understand what I did there, but maybe there is at least one person that will need an explanation:
The algorithm:
Math.random()
function returns a decimal number between 0 and 1 with 16 digits after the decimal fraction point (for
example 0.4363923368509859
).0.6fb7687f
).Math.random().toString(16)
.0.
prefix (0.6fb7687f
=>
6fb7687f
) and get a string with eight hexadecimal
characters long.(Math.random().toString(16).substr(2,8)
.Math.random()
function will return
shorter number (for example 0.4363
), due to zeros at the end (from the example above, actually the number is 0.4363000000000000
). That's why I'm appending to this string "000000000"
(a string with nine zeros) and then cutting it off with substr()
function to make it nine characters exactly (filling zeros to the right).Math.random()
function will return exactly 0 or 1 (probability of 1/10^16 for each one of them). That's why we needed to add nine zeros to it ("0"+"000000000"
or "1"+"000000000"
), and then cutting it off from the second index (3rd character) with a length of eight characters. For the rest of the cases, the addition of zeros will not harm the result because it is cutting it off anyway.Math.random().toString(16)+"000000000").substr(2,8)
.The assembly:
XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
.XXXXXXXX
and -XXXX-XXXX
.XXXXXXXX
-XXXX-XXXX
-XXXX-XXXX
XXXXXXXX
._p8(s)
, the s
parameter tells the function whether to add dashes or not._p8() + _p8(true) + _p8(true) + _p8()
, and return it.Enjoy! :-)