GUIDs in JavaScript

Update: From the comments below it looks like I arrived at the same solution as someone else had  come up with earlier. Recommend you check out the Broofa.com code as they have done more work on making it more performant and robust.

—-

 

 

A while ago I needed a quick and simple way to generate a GUID in a JavaScript project but most of the examples that I could find were either slow, cumbersome or didn’t always pass GUIDs that would pass verification, so I had an attempt at writing my own that had to be performant, small and robust enough to use in a real world environment at scale.

 

Well, after generating 50 million GUIDs across all the mainstream browsers (and some pretty obscure ones!) in my other logging system (an internal project, not jsErrLog – though it’s used there as well) I’m happy that it’s behaving well enough to share so with no further ado…

 

function guid() { // http://www.ietf.org/rfc/rfc4122.txt section 4.4

                return ‘aaaaaaaa-aaaa-4aaa-baaa-aaaaaaaaaaaa’.replace(/[ab]/g, function(ch) {

                                var digit = Math.random()*16|0, newch = ch == ‘a’ ? digit : (digit&0x3|0x8);

                                return newch.toString(16);

                                }).toUpperCase();

}

 

Regular expressions, nested functions and logical operators… probably the most I’ve every crammed into that few characters though if you’re really obsessive you can crunch it down even further to one line at the cost of readability:

 

guid=function(){return”aaaaaaaa-aaaa-4aaa-baaa-aaaaaaaaaaaa”.replace(/[ab]/g,function(ch){var a=Math.random()*16|0;return(ch==”a”?a:a&3|8).toString(16)}).toUpperCase()};

4 Responses to “GUIDs in JavaScript”

  1. Jun Says:

    This code is a copy from "ww.broofa.com/2008/09/javascript-uuid-function/" . You have just changed the variable names and then claim it is your own code… Its not ethical…

  2. Anonymous Says:

    Hi Jun, I’d actually never seen that code before but I agree it does look pretty much the same as the compact sample there. My implementation – like the broofa.com one – was picked from the IETF pseudocode. With something this small it’s not hard to see how we both arrived at the same resultLooks like he’s done a lot more work on making it perform better as well, and evolved it as a node service so I’m going to edit the post to point to the better solution

  3. Broofa Says:

    Hey folks, I’m fine with this. Offbeatmammal reached out to me and explained how he developed this on his own, which I have no reason to disbelieve. The problem is pretty straight-forward. And, while not necessarily glaringly obvious, the solution that we’ve both independently come to is, I believe, is an elegant one that most experienced JS developers would know about. (Real JS ninjas, however, have a very different solution: https://gist.github.com/982883😉 )Anyhow, UUID generation is one of those problem spaces that has a lot of levels to it. The solution(s) can be as trivial or as complex as you choose to make them. It’s always fun seeing how people approach the problem and I’m not surprised more than one person came up with this answer.Cheers,-rwkP.S. FWIW, From RFC4122, section 3, ‘The hexadecimal values "a" through "f" are output as lower case characters and are case insensitive on input.’ 🙂

  4. site Says:

    What’s up, just wanted to tell you, I enjoyed this post.
    It was funny. Keep on posting!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: