GUID generation stackoverflow (v2)

Revision 2 of this benchmark created on


Description

Some tests after reading the stackoverflow question Create GUID / UUID in Javascript

Please read the Stackoverflow answers and comments for context

Preparation HTML

<script>
/**
 * Fast UUID generator, RFC4122 version 4 compliant.
 * @author Jeff Ward (jcward.com).
 * @license MIT license
 * @link http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
 **/
var JeffWard=function(){var e={};var t=[];for(var n=0;n<256;n++){t[n]=(n<16?"0":"")+n.toString(16)}e.generate=function(){var e=Math.random()*4294967295|0;var n=Math.random()*4294967295|0;var r=Math.random()*4294967295|0;var i=Math.random()*4294967295|0;return t[e&255]+t[e>>8&255]+t[e>>16&255]+t[e>>24&255]+"-"+t[n&255]+t[n>>8&255]+"-"+t[n>>16&15|64]+t[n>>24&255]+"-"+t[r&63|128]+t[r>>8&255]+"-"+t[r>>16&255]+t[r>>24&255]+t[i&255]+t[i>>8&255]+t[i>>16&255]+t[i>>24&255]};return e}()</script>

<script>
function Briguy37(){var e=(new Date).getTime();var t="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var n=(e+Math.random()*16)%16|0;e=Math.floor(e/16);return(t=="x"?n:n&3|8).toString(16)});return t}
</script>

<script>
function broofa(){ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=Math.random()*16|0,n=e=="x"?t:t&3|8;return n.toString(16)})}
</script>

<script>
var rfc4122 = (function(){
        var lut = [],
                i = 0, l = 256;

        for(; i < l; ++i){
                lut[i] = (i < 16 ? '0' : '') + (i).toString(16);
        }

        var rfc4122 = function(prng){
                this.prng = prng || Math;

                return this;
        };

        rfc4122.prototype.v4 = function(){
                var d0 = this.prng.random() * 0xffffffff|0,
                        d1 = this.prng.random() * 0xffffffff|0,
                        d2 = this.prng.random() * 0xffffffff|0,
                        d3 = this.prng.random() * 0xffffffff|0;

                return //[
                        lut[d0&0xff] +          lut[d0>>8&0xff] +  lut[d0>>16&0xff] + lut[d0>>24&0xff] + '-' +
                        lut[d1&0xff] +          lut[d1>>8&0xff] +  '-' +
                        lut[d1>>16&0x0f|0x40] + lut[d1>>24&0xff] + '-' +
                        lut[d2&0x3f|0x80] +     lut[d2>>8&0xff] +  '-' +
                        lut[d2>>16&0xff] +      lut[d2>>24&0xff] + lut[d3&0xff] +     lut[d3>>8&0xff] +  lut[d3>>16&0xff] + lut[d3>>24&0xff];
                //].join('');
        };

        return rfc4122;
})();
</script>

Setup

var guid;
    
    try{
    guid = new rfc4122();
    }catch(e){ }

Test runner

Ready to run.

Testing in
TestOps/sec
Jeff Ward
guid = JeffWard.generate();
ready
Briguy37
guid = Briguy37();
ready
broofa
guid = broofa();
ready
tristian
guid.v4();
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.