RSS
 

Posts Tagged ‘guid’

The efficient way to create GUID/UUID in JavaScript (with explanation)

22 May

I want to share with you the fastest GUID/UUID generator method (as far as I know).

Million executions of this implementation takes 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * Generates a GUID string.
 * @returns {String} The generated GUID.
 * @example af8a8416-6e18-a307-bd9c-f2c947bbb3aa
 * @author Slavik Meltser ([email protected]).
 * @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();
}

Read the rest of this entry »