Hi everyone. Here's my attempt:
"use strict"; /*! * JavaScript UUID Generator, v0.0.1 * * Copyright (c) 2009 Massimo Lombardo. * Dual licensed under the MIT and the GNU GPL licenses. */ function UUID() { var uuid = (function () { var i, c = "89ab", u = []; for (i = 0; i < 36; i += 1) { u[i] = (Math.random() * 16 | 0).toString(16); } u[8] = u[13] = u[18] = u[23] = "-"; u[14] = "4"; u[19] = c.charAt(Math.random() * 4 | 0); return u.join(""); })(); return { toString: function () { return uuid; }, valueOf: function () { return uuid; } }; } Generates valid version 4 UUIDs as specified in RFC 4122. Special thanks are due to Robert Kieffer [1], whose example showed me the way. By the way, my code is so damn faster than Robert's... ;-) Particularly, using a standalone V8 build [2], this benchmarking routine (available in the V8 benchmark suite [3]) var benchmark = function (f) { var r = 0, e = 0, s = new Date(); for (r = 0; e < 60000; r += 1) { // runs non-stop for 60 human seconds f(); e = (new Date()) - s; } return ((e * 1000) / r); } and then running print(benchmark(function () { Math.uuid() })); print(benchmark(function () { (new UUID()).toString() })); print(benchmark(function () { (new afdUUID()).toString() //AF-Design implementation [4] })); I get (values are expressed in microseconds) 72.36090726105525 34.95077765480282 43.292667088048624 which means that Robert's code is slower than mine by 107%, while AF-Designs' is by 23.8%. All in all, not bad considering I'm a JavaScript newbie :) Also, minified using the YUI compressor [5] then gzip'd the whole thing weights just 328 bytes! :) As usual, every comment counts and is really welcomed and appreciated. Links: [1] http://www.broofa.com/Tools/Math.uuid.js [2] http://code.google.com/p/v8/ [3] http://code.google.com/apis/v8/benchmarks.html [4] http://af-design.com/services/javascript/uuid/uuid.js [5] http://developer.yahoo.com/yui/compressor/ -- unwiredbrain Linux user #437712