Skip to main content

Class: Snowflake

Defined in: Snowflake.ts:37

A class for generating and deconstructing Twitter snowflakes.

A Twitter snowflake is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.

If we have a snowflake 266241948824764416 we can represent it as binary:

64                                          22     17     12          0
000000111011000111100001101001000101000000 00001 00000 000000000000
number of ms since epoch worker pid increment

Constructors​

Constructor​

new Snowflake(epoch: number | bigint | Date): Snowflake

Defined in: Snowflake.ts:77

Parameters​

ParameterTypeDescription
epochnumber | bigint | Datethe epoch to use

Returns​

Snowflake

Properties​

[EpochNumberSymbol]​

private readonly [EpochNumberSymbol]: number

Defined in: Snowflake.ts:54

Internal

Internal reference of the epoch passed in the constructor as a number


[EpochSymbol]​

private readonly [EpochSymbol]: bigint

Defined in: Snowflake.ts:48

Internal

Internal reference of the epoch passed in the constructor


[IncrementSymbol]​

private [IncrementSymbol]: bigint = 0n

Defined in: Snowflake.ts:60

Internal

Internal incrementor for generating snowflakes


[ProcessIdSymbol]​

private [ProcessIdSymbol]: bigint = 1n

Defined in: Snowflake.ts:66

Internal

The process ID that will be used by default in the generate method


[WorkerIdSymbol]​

private [WorkerIdSymbol]: bigint = 0n

Defined in: Snowflake.ts:72

Internal

The worker ID that will be used by default in the generate method


decode()​

decode: (id: string | bigint) => DeconstructedSnowflake

Defined in: Snowflake.ts:42

Alias for deconstruct

Deconstructs a snowflake given a snowflake ID

Parameters​

ParameterTypeDescription
idstring | bigintthe snowflake to deconstruct

Returns​

DeconstructedSnowflake

a deconstructed snowflake

Example​

const epoch = new Date('2000-01-01T00:00:00.000Z');
const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');

Accessors​

epoch​

Get Signature​

get epoch(): bigint

Defined in: Snowflake.ts:85

The epoch for this snowflake, as a bigint

Returns​

bigint


epochNumber​

Get Signature​

get epochNumber(): number

Defined in: Snowflake.ts:92

The epoch for this snowflake, as a number

Returns​

number


processId​

Get Signature​

get processId(): bigint

Defined in: Snowflake.ts:99

Gets the configured process ID

Returns​

bigint

Set Signature​

set processId(value: number | bigint): void

Defined in: Snowflake.ts:107

Sets the process ID that will be used by default for the generate method

Parameters​
ParameterTypeDescription
valuenumber | bigintThe new value, will be coerced to BigInt and masked with 0b11111n
Returns​

void


workerId​

Get Signature​

get workerId(): bigint

Defined in: Snowflake.ts:114

Gets the configured worker ID

Returns​

bigint

Set Signature​

set workerId(value: number | bigint): void

Defined in: Snowflake.ts:122

Sets the worker ID that will be used by default for the generate method

Parameters​
ParameterTypeDescription
valuenumber | bigintThe new value, will be coerced to BigInt and masked with 0b11111n
Returns​

void

Methods​

deconstruct()​

deconstruct(id: string | bigint): DeconstructedSnowflake

Defined in: Snowflake.ts:174

Deconstructs a snowflake given a snowflake ID

Parameters​

ParameterTypeDescription
idstring | bigintthe snowflake to deconstruct

Returns​

DeconstructedSnowflake

a deconstructed snowflake

Example​

const epoch = new Date('2000-01-01T00:00:00.000Z');
const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');

generate()​

generate(options: SnowflakeGenerateOptions): bigint

Defined in: Snowflake.ts:138

Generates a snowflake given an epoch and optionally a timestamp

Parameters​

ParameterTypeDescription
optionsSnowflakeGenerateOptionsoptions to pass into the generator, see SnowflakeGenerateOptions note when increment is not provided it defaults to the private increment of the instance

Returns​

bigint

A unique snowflake

Example​

const epoch = new Date('2000-01-01T00:00:00.000Z');
const snowflake = new Snowflake(epoch).generate();

timestampFrom()​

timestampFrom(id: string | bigint): number

Defined in: Snowflake.ts:192

Retrieves the timestamp field's value from a snowflake.

Parameters​

ParameterTypeDescription
idstring | bigintThe snowflake to get the timestamp value from.

Returns​

number

The UNIX timestamp that is stored in id.


compare()​

static compare(a: string | bigint, b: string | bigint): -1 | 0 | 1

Defined in: Snowflake.ts:215

Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given snowflake in sort order.

Parameters​

ParameterTypeDescription
astring | bigintThe first snowflake to compare.
bstring | bigintThe second snowflake to compare.

Returns​

-1 | 0 | 1

-1 if a is older than b, 0 if a and b are equals, 1 if a is newer than b.

Examples​

const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
console.log(ids.sort((a, b) => Snowflake.compare(a, b)));
// → ['254360814063058944', '737141877803057244', '1056191128120082432'];
const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));
// → ['1056191128120082432', '737141877803057244', '254360814063058944'];