Friday, November 14th, 2008

Guid0: JavaScript GUIDs

Category: JavaScript, Library, Tip

<>p>Our own Michael Mahemoff is at it again, creating a simple little GUID generator called Guid0:

Guid0 is a GUID library for Javascript. Okay, it doesn’t yet do official, bona fide, 128-bit, GUIDs yet, mainly for API design reasons. But this is a library you might find useful if you want to generate a unique ID in your Ajax app.

javascript
< view plain text >
  1. // simple
  2. guid = new Guid();
  3. var newguid = guid.generate();
  4.  
  5. // options
  6. guid = new Guid(
  7.   {
  8.     chars: Guid.constants.base85,  // or you could say "abc" if you only wanted those chars to appear
  9.     epoch: "June 1, 2003",
  10.     counterSequenceLength: 2, // a counter field appended to the end
  11.     randomSequenceLength: 2 // a random field appended to the end
  12.   }
  13. );

He is working on 128-bit support.

Related Content:

Posted by Dion Almaer at 6:47 am
3 Comments

+++--
3.1 rating from 20 votes

3 Comments »

Comments feed TrackBack URI

Is it just me or isn’t guids supposed to be globally unique and include the mac address of the computer or something else that seeds the key generation for a specific user session. I know this isn’t possible to do using Javascript but if the guid isn’t really unique whats the point of using it. When the same guid can be generated on two separate machines by accident. At least it shouldn’t be called guid since it doesn’t live up to it’s name.

Comment by Spocke — November 14, 2008

Spocke, that’s correct. One could try and use the IP number in place of the MAC address (or a cookie if available), for example, but as with the MAC solutions, there are privacy and security concerns. You can deal with them to some extent using encryption techniques, leading to a long hash, but my immediate objective with this library was to create small strings.

With a 128-bit GUID, though, the probability of a collision is extremely low. There’s 32 bits for the timestamp (until 2038) and 96 left over. There’s also a counter sequence of several bits to ensure an individual client doesn’t collide with itself, leaving around 80-90 bits for randomness. So if you and someone else generate a timestamp in the same MILLISECOND, already very unlikely, you have a 1 in 2^86 chance of a collision. For most systems, such as the comments plugin I’m working on, this is something I can live with, and in many cases worth the trade-off over using an IP address. (Needs further back-of-the-napkin probability analysis!).

This all assumes a good random number algorithm. Since Javascript doesn’t actually let you seed randomness, that’s a serious concern, and it may be wise to use an API like this http://json-random.appjet.net/ where accessible.

Comment by Michael Mahemoff — November 14, 2008

In a shameless act of self-promotion, I’ll point readers at the randomUUID.js script I created, which was covered in Ajaxian here. Originally designed to produce RFC 4122-compliant IDs, it can also generate non-standard IDs of any length using 62 character base. There’s a demo here, which shows usage, resulting IDs, and the possible number of ids for each invocation.

@Michael: You should check out the “version 4″ format of GUID allowed by RFC4122. That’s what the randomUUID.js script produces, and it is trivial to generate. It’s also a *much* more appropriate form for javascript where timestamps and unique values (based on IP address or whatever) are not available. It’s also has a much larger UUID “space” since 122 of the 128 bits are randomly generated.

Regarding your API, I think you can significantly simplify it by using the module pattern. This will let you make all the constants truely private. More importantly, you can make your log() method private as well, and thereby avoid clobbering any log method that users of your script may have defined.

Comment by broofa — November 15, 2008

Leave a comment

You must be logged in to post a comment.