Skip to content

Instantly share code, notes, and snippets.

@kerryChen95
Created December 6, 2013 15:43
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 kerryChen95/7826826 to your computer and use it in GitHub Desktop.
Save kerryChen95/7826826 to your computer and use it in GitHub Desktop.
create unique id and check duplication.
function check() {
var hash = {},
key,
times = Math.pow(10, 9),
i = 1;
do {
key = create3();
if (hash[key]) {
console.log('duplicate at the %d times', i);
return i;
}
hash[key] = true;
if (i % 10000 === 0) {
console.log('the %d time pass', i);
}
}
while (i++ !== times)
}
function create1() {
var t = (new Date()).getTime(),
id = (parseInt(Math.random() * (Math.pow(10, t.toString().length) - 1)) ^ t);
return Math.abs(id).toString(16);
}
function create2() {
return Math.random().toString(36).substr(2, 9);
}
function create3() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
(function () {
var i = 0,
times = 20,
sum = 0;
do {
sum += check();
}
while (i++ !== times)
console.log('average deplicate times: %d', sum / times);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment