guid.js
 1  'use strict';
 2  
 3  var now       = Date.now
 4  
 5  // To make id smaller we get miliseconds count from more recent date
 6    , start     = Date.UTC(2011, 8, 21)
 7  
 8  // Prefix with number, it reduces chances of collision with variable names
 9  // (helpful if used as property names on objects)
10    , prefix    = String(Math.floor(Math.random() * 10))
11  
12  // Make it more unique
13    , postfix   = Math.floor(Math.random() * 36).toString(36)
14  
15  // Cache used timestamps to prevent duplicate creation
16    , generated = {};
17  
18  module.exports = function () {
19  	var id = now() - start;
20  	while (generated.hasOwnProperty(id)) {
21  		++id;
22  	}
23  	generated[id] = true;
24  	return prefix + id.toString(36) + postfix;
25  };