Snowflake ID Generation in JavaScript with simple-flakeid

Introduction

The simple-flakeid library provides an implementation of the Snowflake ID generation algorithm in JavaScript. This open-source project is based on the yitter ID generator algorithm, with optimizations while maintaining the open-source principle.

Installation

Install the package using npm:

npm install simple-flakeid

Core Functions

  • NextId(): Returns either a number or bigint depending on whether the value exceeds JavaScript's maximum safe integer limit
  • NextNumber(): Always returns a numbeer type, throws an error if the value would exceed the maximum safe integer
  • NextBigId(): Always returns a bigint type

JavaScript Usage Examples

Using NextId() Method

const { SnowflakeIdv1 } = require('simple-flakeid');
const generator = new SnowflakeIdv1({ workerId: 1 });

for (let i = 0; i < 10; i++) {
    const uniqueId = generator.NextId();
    console.log(`${i} ID:${uniqueId} ${typeof uniqueId} length:${uniqueId.toString().length}`);
}

Sample output:

0 ID:235100305752133 number length:15
1 ID:235100305772613 number length:15
2 ID:235100305776709 number length:15
3 ID:235100305776710 number length:15
4 ID:235100305776711 number length:15
5 ID:235100305776712 number length:15
6 ID:235100305776713 number length:15
7 ID:235100305780805 number length:15
8 ID:235100305780806 number length:15
9 ID:235100305780807 number length:15

Using NextBigId() Method

const { SnowflakeIdv1 } = require('simple-flakeid');
const generator = new SnowflakeIdv1({ workerId: 1 });

for (let i = 0; i < 10; i++) {
    const uniqueId = generator.NextBigId();
    console.log(`${i} ID:${uniqueId} ${typeof uniqueId} length:${uniqueId.toString().length}`);
}

Sample output:

0 ID:235104756604997 bigint length:15
1 ID:235104756629573 bigint length:15
2 ID:235104756629574 bigint length:15
3 ID:235104756629575 bigint length:15
4 ID:235104756629576 bigint length:15
5 ID:235104756629577 bigint length:15
6 ID:235104756633669 bigint length:15
7 ID:235104756633670 bigint length:15
8 ID:235104756633671 bigint length:15
9 ID:235104756633672 bigint length:15

TypeScript Usage

import { SnowflakeIdv1 } from "simple-flakeid";

const generator = new SnowflakeIdv1({ workerId: 1 });

for (let i = 0; i < 10; i++) {
    const uniqueId = generator.NextId();
    console.log(`${i} ID:${uniqueId} ${typeof uniqueId} length:${uniqueId.toString().length}`);
}

Sample output:

0 ID:235099434217541 number length:15
1 ID:235099434225733 number length:15
2 ID:235099434225734 number length:15
3 ID:235099434225735 number length:15
4 ID:235099434225736 number length:15
5 ID:235099434229829 number length:15
6 ID:235099434229830 number length:15
7 ID:235099434229831 number length:15
8 ID:235099434229832 number length:15
9 ID:235099434233925 number length:15

Database Considerations

When storing these IDs in MySQL, note that the standard INT type supports only 10 digits. Since Snowflake IDs are 11-15 digits by default, you should use the BIGINT data type in your database schema to acommodate these values.

Additional Resources

For more comprehensive examples, including handling bigint length issues in API implementations, refer to the demo repository: https://github.com/zhupengfeivip/simple-flakeId-demo.git

Tags: snowflake-id javascript TypeScript nodejs id-generation

Posted on Tue, 22 Sep 2026 16:52:31 +0000 by harkly