Js Snowflake(雪花算法)生成随机ID的实现方法
1、snowflake-id插件
importSnowflakeIdfrom"snowflake-id";
constguid=num=>{
constid=newSnowflakeId();
returnid.generate();
};
2、原生使用
varSnowflake=/**@class*/(function(){
functionSnowflake(_workerId,_dataCenterId,_sequence){
this.twepoch=1288834974657n;
//this.twepoch=0n;
this.workerIdBits=5n;
this.dataCenterIdBits=5n;
this.maxWrokerId=-1n^(-1n<this.maxWrokerId||this.workerId<0){
thrownewError('_workerIdmustmaxthan0andsmallthanmaxWrokerId-['+this.maxWrokerId+']');
}
if(this.dataCenterId>this.maxDataCenterId||this.dataCenterId<0){
thrownewError('_dataCenterIdmustmaxthan0andsmallthanmaxDataCenterId-['+this.maxDataCenterId+']');
}
this.workerId=BigInt(_workerId);
this.dataCenterId=BigInt(_dataCenterId);
this.sequence=BigInt(_sequence);
}
Snowflake.prototype.tilNextMillis=function(lastTimestamp){
vartimestamp=this.timeGen();
while(timestamp<=lastTimestamp){
timestamp=this.timeGen();
}
returnBigInt(timestamp);
};
Snowflake.prototype.timeGen=function(){
returnBigInt(Date.now());
};
Snowflake.prototype.nextId=function(){
vartimestamp=this.timeGen();
if(timestamp
控制台输出1141531990672150528n为bigint格式,.toString()转为字符串格式即可
3、ES6使用
importbigIntfrom"big-integer";
constguid=()=>{
constSnowflake=/**@class*/(function(){
functionSnowflake(_workerId,_dataCenterId,_sequence){
//this.twepoch=1288834974657;
this.twepoch=0;
this.workerIdBits=5;
this.dataCenterIdBits=5;
this.maxWrokerId=-1^(-1<this.maxWrokerId||this.workerId<0){
thrownewError(
'config.worker_idmustmaxthan0andsmallthanmaxWrokerId-['+this.maxWrokerId+']'
);
}
if(this.dataCenterId>this.maxDataCenterId||this.dataCenterId<0){
thrownewError(
'config.data_center_idmustmaxthan0andsmallthanmaxDataCenterId-['+
this.maxDataCenterId+
']'
);
}
this.workerId=_workerId;
this.dataCenterId=_dataCenterId;
this.sequence=_sequence;
}
Snowflake.prototype.tilNextMillis=function(lastTimestamp){
vartimestamp=this.timeGen();
while(timestamp<=lastTimestamp){
timestamp=this.timeGen();
}
returntimestamp;
};
Snowflake.prototype.timeGen=function(){
//newDate().getTime()===Date.now()
returnDate.now();
};
Snowflake.prototype.nextId=function(){
vartimestamp=this.timeGen();
if(timestamp
guid()即可调用
4、多次重复调用出现一样id的bug
console.log(guid(),newDate().getTime());
console.log(guid(),newDate().getTime());
console.log(guid(),newDate().getTime());
console.log(guid(),newDate().getTime());
console.log(guid(),newDate().getTime());
console.log(guid(),newDate().getTime());
console.log(guid(),newDate().getTime());
console.log(guid(),newDate().getTime());
console.log(guid(),newDate().getTime());
console.log(guid(),newDate().getTime());
console.log(guid(),newDate().getTime());
console.log(guid(),newDate().getTime());
修改如下
importSnowflakeIdfrom"snowflake-id";
constguid=num=>{
constsnowflake=newSnowflakeId();
letarr=[];
for(leti=0;i
单个调用guid()
n个调用guid(n)
文档
到此这篇关于JsSnowflake(雪花算法)生成随机ID的实现方法的文章就介绍到这了,更多相关Js雪花算法生成随机ID内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。