diff --git a/JavaScript/test/test2.js b/JavaScript/test/test2.js index 1aa6048c0293a15740d8bd3eae96da3d8b972e06..1e7fd4b7d5f60b40498b8aa27a6de098f056980f 100644 --- a/JavaScript/test/test2.js +++ b/JavaScript/test/test2.js @@ -8,10 +8,11 @@ function test1() { } } function test2() { - const genid = new GenId({ WorkerId: 1 }) - const id = genid.NextId() - console.log(typeof (id)) - console.log(id, id.toString().length) + const genid = new GenId({ WorkerId: 1, SeqBitLength: 14 }) + for (let i = 0; i < 10; i++) { + let id1 = genid.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) + } } function main() { diff --git a/TypeScript/README.md b/TypeScript/README.md index 00e643e0bf9b17068dc131ae18e3ffddad8a0b8a..f0e47b51a680e9925a8e9a6a70d3213e8a058c1e 100644 --- a/TypeScript/README.md +++ b/TypeScript/README.md @@ -6,6 +6,10 @@ 代码贡献者:zhupengfei(在 bubao 布宝 的JS基础上改版,感谢bubao 布宝) +js Number 类型最大数值:9007199254740992(16位), + +在JS中没有bigint类型,所以建议将ID控制在16位以内,统一使用number类型 + 执行测试代码 @@ -22,12 +26,155 @@ import { snowflakeIdv1 } from '../snowflakeIdv1' const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId -const Method = process.env.Method == undefined ? 1 : process.env.Method - -let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, Method: Method }) +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId}) let id1 = gen1.NextId() console.log(id1, id1.toString().length) ``` +## 示例 + +```js +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 6 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} 长度:${id1.toString().length}`) +} + +$ ts-node test/test4.ts +0 ID:234712552579141 长度:15 +1 ID:234712552587333 长度:15 +2 ID:234712552587334 长度:15 +3 ID:234712552587335 长度:15 +4 ID:234712552587336 长度:15 +5 ID:234712552591429 长度:15 +6 ID:234712552591430 长度:15 +7 ID:234712552591431 长度:15 +8 ID:234712552591432 长度:15 +9 ID:234712552591433 长度:15 + + +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 11 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +} + + +$ ts-node test/test4.ts +0 ID:7510958933018629 number 长度:16 +1 ID:7510958933280773 number 长度:16 +2 ID:7510958933280774 number 长度:16 +3 ID:7510958933280775 number 长度:16 +4 ID:7510958933411845 number 长度:16 +5 ID:7510958933411846 number 长度:16 +6 ID:7510958933542917 number 长度:16 +7 ID:7510958933542918 number 长度:16 +8 ID:7510958933542919 number 长度:16 +9 ID:7510958933673989 number 长度:16 + +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 12 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +} + +$ ts-node test/test4.ts +0 ID:15021931987734533 bigint 长度:17 +1 ID:15021931987996677 bigint 长度:17 +2 ID:15021931987996678 bigint 长度:17 +3 ID:15021931987996679 bigint 长度:17 +4 ID:15021931987996680 bigint 长度:17 +5 ID:15021931988258821 bigint 长度:17 +6 ID:15021931988258822 bigint 长度:17 +7 ID:15021931988258823 bigint 长度:17 +8 ID:15021931988258824 bigint 长度:17 +9 ID:15021931988520965 bigint 长度:17 + + +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 13 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +} + +$ ts-node test/test4.ts +0 ID:30043877337997317 bigint 长度:17 +1 ID:30043877338521605 bigint 长度:17 +2 ID:30043877338521606 bigint 长度:17 +3 ID:30043877339045893 bigint 长度:17 +4 ID:30043877339045894 bigint 长度:17 +5 ID:30043877339045895 bigint 长度:17 +6 ID:30043877339045896 bigint 长度:17 +7 ID:30043877339570181 bigint 长度:17 +8 ID:30043877339570182 bigint 长度:17 +9 ID:30043877339570183 bigint 长度:17 + + + + + + + + + + + + + + + + +``` + +## 同时兼容number和bigint的写法 + +如果您觉得这个用法更好,可以手动替换对应方法 + +```js + /** + * 生成ID + * @returns + */ + public NextId(): number | bigint { + if (this._IsOverCost) { + // + let id = this.NextOverCostId() + if (id >= 9007199254740992n) + return id + else + return parseInt(id.toString()) + } else { + // + let id = this.NextNormalId() + if (id >= 9007199254740992n) + return id + else + return parseInt(id.toString()) + } + } + +``` + +## 其他帮助 + +在mysql中int类型最大长度是10位数字,由于本算法默认生成的是15位,最短也是11位,所以在mysql中需要使用bigint数据类型 + + diff --git a/TypeScript/snowflakeIdv1.ts b/TypeScript/snowflakeIdv1.ts index eb1aff449a1d9a25122026faf1fb7e908c370c1d..f84a22f144c3d3b31b6d10f7c5a71d6e1122f4b6 100644 --- a/TypeScript/snowflakeIdv1.ts +++ b/TypeScript/snowflakeIdv1.ts @@ -328,13 +328,13 @@ export class snowflakeIdv1 { * 生成ID * @returns */ - public NextId(): number { - if (this.Method == BigInt(1)) { - //雪花漂移算法 - return parseInt(this.NextOverCostId().toString()) + public NextId(): bigint { + if (this._IsOverCost) { + // + return this.NextOverCostId() } else { - //常规雪花算法 - return parseInt(this.NextNormalId().toString()) + // + return this.NextNormalId() } } } diff --git a/TypeScript/test/test1.ts b/TypeScript/test/test1.ts index 19575670df9ae180ca76523d82b8b9461799dca2..5041b27792073bbf825a4d9d6d9961ac08c433df 100644 --- a/TypeScript/test/test1.ts +++ b/TypeScript/test/test1.ts @@ -2,8 +2,6 @@ import { snowflakeIdv1 } from '../snowflakeIdv1' const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId -const Method = process.env.Method == undefined ? 1 : process.env.Method - -let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, Method: Method }) +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId }) let id1 = gen1.NextId() -console.log(id1, id1.toString().length) \ No newline at end of file +console.log(`ID:${id1} 长度:${id1.toString().length}`) \ No newline at end of file diff --git a/TypeScript/test/test3.ts b/TypeScript/test/test3.ts new file mode 100644 index 0000000000000000000000000000000000000000..19575670df9ae180ca76523d82b8b9461799dca2 --- /dev/null +++ b/TypeScript/test/test3.ts @@ -0,0 +1,9 @@ +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +const Method = process.env.Method == undefined ? 1 : process.env.Method + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, Method: Method }) +let id1 = gen1.NextId() +console.log(id1, id1.toString().length) \ No newline at end of file diff --git a/TypeScript/test/test4.ts b/TypeScript/test/test4.ts new file mode 100644 index 0000000000000000000000000000000000000000..ab51f36ba6d89ed513f9d12d98eb9cbdac59e461 --- /dev/null +++ b/TypeScript/test/test4.ts @@ -0,0 +1,9 @@ +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 10 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +} diff --git a/TypeScript/test/test5.ts b/TypeScript/test/test5.ts new file mode 100644 index 0000000000000000000000000000000000000000..5877acacdb2439b3111f2a9d3f94700c41a93448 --- /dev/null +++ b/TypeScript/test/test5.ts @@ -0,0 +1,18 @@ +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 10 }) +// for (let i = 0; i < 10; i++) { +// let id1 = gen1.NextId() +// console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +// } + +console.time("Test Run") +const HSet = new Set() +for (let index = 0; index < 500000; index++) { + HSet.add(gen1.NextId()) +} +console.timeEnd("Test Run") +console.log([...HSet.values()].join("\n")) +console.log(HSet.size) \ No newline at end of file