Skip to content

Instantly share code, notes, and snippets.

@Merovex
Forked from kaizhu256/gist:4482069
Created December 2, 2016 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Merovex/16b584a1cfab090d2b81fefb14987ed7 to your computer and use it in GitHub Desktop.
Save Merovex/16b584a1cfab090d2b81fefb14987ed7 to your computer and use it in GitHub Desktop.
javascript - very fast and simple uuid4 generator benchmarked on http://jsperf.com/uuid4/8
/*jslint bitwise: true, indent: 2, nomen: true, regexp: true, stupid: true*/
(function () {
'use strict';
var exports = {};
exports.uuid4 = function () {
//// return uuid of form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
var uuid = '', ii;
for (ii = 0; ii < 32; ii += 1) {
switch (ii) {
case 8:
case 20:
uuid += '-';
uuid += (Math.random() * 16 | 0).toString(16);
break;
case 12:
uuid += '-';
uuid += '4';
break;
case 16:
uuid += '-';
uuid += (Math.random() * 4 | 8).toString(16);
break;
default:
uuid += (Math.random() * 16 | 0).toString(16);
}
}
return uuid;
};
//// test
console.log(exports.uuid4());
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment