snowflake-generator

A 64-bit Snowflake generator written in pure js

Usage no npm install needed!

<script type="module">
  import snowflakeGenerator from 'https://cdn.skypack.dev/snowflake-generator';
</script>

README

snowflake-generator

NPM version NPM downloads Patreon

A lightweight (12KB) Twitter Snowflake generation package utilising ES6 Promises.


Getting Started

Install the npm package

npm install snowflake-generator

Require the package into your code

const SnowflakeGenerator = require('snowflake-generator');

Construct a new Snowflake Generator with the EPOCH timestamp in milliseconds

const Snowflake = new SnowflakeGenerator(1420070400000); // Thursday, 1 January 2015 00:00:00

Generate a Snowflake

const flake = Snowflake.generate();

Alternatively for a performance test you can run the example.js file which console logs the duration it took to generate 1000 Snowflakes and how many are duplicated.

node example.js // Generated 1000 Snowflakes in 29 ms. with 0 duplicates

SnowflakeGenerator (class)

Constructor

new SnowflakeGenerator(epoch);
param type optional default description
epoch Date|Number false Custom EPOCH timestamp in milliseconds
total_bits Number true 64 Number of bits a generated Snowflakes id is made of. The combined amount of the epoch_bits, worker_bits, process_bits and increment_bits should be less than or equal to this value.
epoch_bits Number true 42 Number of bits a generated Snowflakes timestamp is stored in.
worker_bits Number true 5 Number of bits a generated Snowflakes worker id is stored in.
process_bits Number true 5 Number of bits a generated Snowflakes process id is stored in.
increment_bits Number true 12 Number of bits a generated Snowflakes increment value is stored in.

Properties

.EPOCH

Custom EPOCH timestamp
@type Number
@readonly

.TOTAL_BITS

Total number of bits a Snowflake id is made of
@type Number
@readonly

.EPOCH_BITS

Number of bits a Snowflakes EPOCH timestamp uses
@type Number
@readonly

.MAX_EPOCH

Maximum possible EPOCH timestamp
@type Number
@readonly

.WORKER_BITS

Number of bits a Snowflakes worker id uses
@type Number
@readonly

.MAX_WORKER

Maximum possible worker id
@type Number
@readonly

.WORKER_ID

The generators worker id
@type Number
@readonly

.PID_BITS

Number of bits a Snowflakes process id uses
@type Number
@readonly

.MAX_PID

Maximum possible process id
@type Number
@readonly

.PROCESS_ID

The generators process id
@type Number
@readonly

.INCREMENT_BITS

Number of bits a Snowflakes increment uses
@type Number
@readonly

.MAX_INCREMENT

Maximum possible increment
@type Number
@readonly

.INCREMENT

The generators increment number
@type Number

Methods

.generate(options)

Generate a single Snowflake | parameter | type | optional | description | | :--------: | :----------: | :------: | :------------: | :---------- | | options | SnowflakeOptions | true | Snowflake generation options @returns Snowflake

.generateMany(amount, options)

Generate a defined amount of Snowflakes and returns them in an array.

Note: Using this method generates Snowflakes without control over its more specific inputs like the generate method allows.

parameters type optional description
amount Number false Number of Snowflakes to generate
options SnowflakeOptions true Snowflake generation options
@returns Promise<Array<Snowflake>>

.deconstruct(snowflake)

Deconstruct a Snowflake into its stored values using the generators EPOCH timestamp | parameters | type | description | | :--------: | :------------: | :---------- | | snowflake | String|Number | Snowflake id to deconstruct @returns Object


Snowflake (class)

Constructor

new Snowflake(generator, timestamp)

Properties

.generator

The Snowflakes generator
@type SnowflakeGenerator
@readonly

.snowflake

The Snowflake id
@type String

.timestamp

The Snowflakes stored EPOCH timestamp
@type Number
@readonly

.worker_id

The Snowflakes stored worker id
@type Number
@readonly

.process_id

The Snowflakes stored process id
@type Number
@readonly

.increment

The Snowflakes stored increment value
@type Number
@readonly

.binary

The Snowflakes id in binary format
@type String
@readonly

Methods

.deconstruct()

Deconstructs the Snowflake into its stored values
@returns Object


Snowflake Options (object)

Properties

timestamp

The EPOCH timestamp, defaults to current timestamp
@type Date|Number

worker_id

The Worker ID, defaults to the generators worker id, or 1
@type Number

process_id

The Procces ID, defaults to the generators process id, or 0
@type Number

increment

The timestamps generation increment value
@type Number