【JavaScript】使用ES6实现雪花算法

发布时间:2023-06-06 09:34:18

Snowflake.js

/** * 获取ID的雪花算法 * * @author: tanpeng * @since : 2020/7/24 15:00 */class Snowflake {  constructor(_workerId, _dataCenterId, _sequence) {    this.twepoch = 1288834974657n;    // this.twepoch = 0n;    this.workerIdBits = 5n;    this.dataCenterIdBits = 5n;    this.maxWrokerId = -1n ^ (-1n << this.workerIdBits); // 值为:31    this.maxDataCenterId = -1n ^ (-1n << this.dataCenterIdBits); // 值为:31    this.sequenceBits = 12n;    this.workerIdShift = this.sequenceBits; // 值为:12    this.dataCenterIdShift = this.sequenceBits + this.workerIdBits; // 值为:17    this.timestampLeftShift = this.sequenceBits + this.workerIdBits + this.dataCenterIdBits; // 值为:22    this.sequenceMask = -1n ^ (-1n << this.sequenceBits); // 值为:4095    this.lastTimestamp = -1n;    //设置默认值,从环境变量中提取    this.workerId = 1n;    this.dataCenterId = 1n;    this.sequence = 0n;    if (this.workerId > this.maxWrokerId || this.workerId < 0) {      throw new Error(`_workerId must max than 0 and small than maxWrokerId-[${this.maxWrokerId}]`);    }    if (this.dataCenterId > this.maxDataCenterId || this.dataCenterId < 0) {      throw new Error(`_dataCenterId must max than 0 and small than maxDataCenterId-[${this.maxDataCenterId}]`);    }    this.workerId = BigInt(_workerId);    this.dataCenterId = BigInt(_dataCenterId);    this.sequence = BigInt(_sequence);  }  tilNextMillis(lastTimestamp) {    let timestamp = this.timeGen();    while (timestamp <= lastTimestamp) {      timestamp = this.timeGen();    }    return BigInt(timestamp);  }  timeGen() {    return BigInt(Date.now());  }  nextId() {    let timestamp = this.timeGen();    if (timestamp < this.lastTimestamp) {      throw new Error(`Clock moved backwards. Refusing to generate id for ${this.lastTimestamp - timestamp}`);    }    if (this.lastTimestamp === timestamp) {      this.sequence = (this.sequence + 1n) & this.sequenceMask;      if (this.sequence === 0n) {        timestamp = this.tilNextMillis(this.lastTimestamp);      }    } else {      this.sequence = 0n;    }    this.lastTimestamp = timestamp;    return ((timestamp - this.twepoch) << this.timestampLeftShift) |      (this.dataCenterId << this.dataCenterIdShift) |      (this.workerId << this.workerIdShift) |      this.sequence;  }}
Usage
console.time();const snowflake = new Snowflake(1n, 1n, 0n);const tempIds = [];for (let i = 0; i < 10000; i++) {  const id = snowflake.nextId();  console.log(id);  if (tempIds.indexOf(id) < 0) {    tempIds.push(id);  }}console.log(tempIds.length);console.timeEnd();
疑问
  • 雪花算法生成的ID是多少?是固定位数还是会改变?
  • twepoch为什么所有的起始值都是1288834974657

上一篇 【JUC】DelayQueue了解一下
下一篇 【Navicat】Cannot load OCI DLL, 87

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: Java教程Java基础Java编程技巧面试题Java面试题