Documentation / @andrew_l/snowflake / Snowflake
Class: Snowflake
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(
opts
):Snowflake
Parameters
opts
number
| bigint
| SnowflakeOptions
| Date
Returns
Snowflake
Properties
decode()
decode: (
id
) =>DeconstructedSnowflake
Alias for deconstruct
Deconstructs a snowflake given a snowflake ID
Parameters
id
the snowflake to deconstruct
string
| bigint
| Uint8Array
Returns
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():
number
The epoch for this snowflake, as a number
Returns
number
processId
Get Signature
get processId():
number
Gets the configured process ID
Returns
number
Set Signature
set processId(
value
):void
Sets the process ID that will be used by default for the generate method
Parameters
value
The new value, will be masked with 0b11111
number
| bigint
Returns
void
workerId
Get Signature
get workerId():
number
Gets the configured worker ID
Returns
number
Set Signature
set workerId(
value
):void
Sets the worker ID that will be used by default for the generate method
Parameters
value
The new value, will be masked with 0b11111
number
| bigint
Returns
void
Methods
deconstruct()
deconstruct(
id
):DeconstructedSnowflake
Deconstructs a snowflake given a snowflake ID
Parameters
id
the snowflake to deconstruct
string
| bigint
| Uint8Array
Returns
a deconstructed snowflake
Example
const epoch = new Date('2000-01-01T00:00:00.000Z');
const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
generate()
generate(
timestamp
):bigint
Generates a Snowflake ID as a Uint8Array
buffer.
Parameters
timestamp
number
| Date
Returns
bigint
generateBuffer()
generateBuffer(
timestamp
):Uint8Array
Generates a snowflake given an epoch and optionally a timestamp
Parameters
timestamp
number
| Date
Returns
Uint8Array
A unique snowflake as Uint8Array
Example
const epoch = new Date('2000-01-01T00:00:00.000Z');
const snowflake = new Snowflake({ epoch }).generate();
generateBufferUnsafe()
generateBufferUnsafe(
timestamp
):Uint8Array
Generates a Snowflake ID as a Uint8Array
buffer.
⚠️ Returns a reference to the same internal Uint8Array
instance. Its contents may be overwritten on the next ID generation call.
This method is intended for high-performance scenarios where you immediately transform the buffer (e.g., to base62) before calling again. Avoid storing or mutating the returned buffer directly.
Parameters
timestamp
number
| Date
Returns
Uint8Array
timestampFrom()
timestampFrom(
id
):number
Retrieves the timestamp field's value from a snowflake.
Parameters
id
The snowflake to get the timestamp value from.
string
| bigint
| Uint8Array
Returns
number
The UNIX timestamp that is stored in id
.
bufferFrom()
static
bufferFrom(value
):Uint8Array
Parse value as Uint8Array buffer
Parameters
value
string
| bigint
Returns
Uint8Array
compare()
static
compare(a
,b
):-1
|0
|1
Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given snowflake in sort order.
Parameters
a
The first snowflake to compare.
string
| bigint
| Uint8Array
b
The second snowflake to compare.
string
| bigint
| Uint8Array
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
.