README
snowflake-generator
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
(class)
SnowflakeGenerator 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
@typeNumber
@readonly
.TOTAL_BITS
Total number of bits a Snowflake id is made of
@typeNumber
@readonly
.EPOCH_BITS
Number of bits a Snowflakes EPOCH timestamp uses
@typeNumber
@readonly
.MAX_EPOCH
Maximum possible EPOCH timestamp
@typeNumber
@readonly
.WORKER_BITS
Number of bits a Snowflakes worker id uses
@typeNumber
@readonly
.MAX_WORKER
Maximum possible worker id
@typeNumber
@readonly
.WORKER_ID
The generators worker id
@typeNumber
@readonly
.PID_BITS
Number of bits a Snowflakes process id uses
@typeNumber
@readonly
.MAX_PID
Maximum possible process id
@typeNumber
@readonly
.PROCESS_ID
The generators process id
@typeNumber
@readonly
.INCREMENT_BITS
Number of bits a Snowflakes increment uses
@typeNumber
@readonly
.MAX_INCREMENT
Maximum possible increment
@typeNumber
@readonly
.INCREMENT
The generators increment number
@typeNumber
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
(class)
Snowflake Constructor
new Snowflake(generator, timestamp)
Properties
.generator
The Snowflakes generator
@typeSnowflakeGenerator
@readonly
.snowflake
The Snowflake id
@typeString
.timestamp
The Snowflakes stored EPOCH timestamp
@typeNumber
@readonly
.worker_id
The Snowflakes stored worker id
@typeNumber
@readonly
.process_id
The Snowflakes stored process id
@typeNumber
@readonly
.increment
The Snowflakes stored increment value
@typeNumber
@readonly
.binary
The Snowflakes id in binary format
@typeString
@readonly
Methods
.deconstruct()
Deconstructs the Snowflake into its stored values
@returnsObject
(object)
Snowflake Options Properties
timestamp
The EPOCH timestamp, defaults to current timestamp
@typeDate|Number
worker_id
The Worker ID, defaults to the generators worker id, or 1
@typeNumber
process_id
The Procces ID, defaults to the generators process id, or 0
@typeNumber
increment
The timestamps generation increment value
@typeNumber