npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

flake-idgen-63

v1.0.1

Published

63-Bit Flake ID generator yields k-ordered, conflict-free ids in a distributed environment.

Downloads

11

Readme

flake-idgen-63

npm version Build Status Coverage Status Code Climate Code Documentation Issue Count Dependency Status npm downloads GitHub Issues License Cat Gifs

flake-idgen-63 yields 64-Bit (with 63 signigicant bits) k-ordered, conflict-free ids in a distributed environment.

Installation

$ npm install --save flake-idgen-63 ⏎

Format

The flake-idgen-63 format is made up of: timestamp, datacenter, worker and counter. Examples in the following table:

+--------------------------+------------+--------+---------+---------------------+
|        Timestamp         | Datacenter | Worker | Counter |   Flake ID (HEX)    |
+--------------------------+------------+--------+---------+---------------------+
| [2013, 3, 1, 0, 0, 0, 0] |      0     |    0   |    0    | 27b8 5c96 0000 0000 |
+--------------------------+------------+--------+---------+---------------------+
| [2013, 3, 1, 0, 0, 0, 0] |      0     |    0   |    1    | 27b8 5c96 0000 0001 |
+--------------------------+------------+--------+---------+---------------------+
| [2013, 3, 1, 0, 0, 0, 0] |      0     |    0   |    2    | 27b8 5c96 0000 0002 |
+--------------------------+------------+--------+---------+---------------------+
| [2013, 3, 1, 0, 0, 0, 0] |      0     |    1   |    0    | 27b8 5c96 0000 1000 |
+--------------------------+------------+--------+---------+---------------------+
| [2013, 3, 1, 0, 0, 0, 0] |      0     |    1   |    1    | 27b8 5c96 0000 1001 |
+--------------------------+------------+--------+---------+---------------------+
| [2013, 3, 1, 0, 0, 0, 1] |      0     |    0   |    0    | 27b8 5c96 0020 0000 |
+--------------------------+------------+--------+---------+---------------------+
| [2013, 3, 1, 0, 0, 0, 1] |      0     |    0   |    1    | 27b8 5c96 0020 0001 |
+--------------------------+------------+--------+---------+---------------------+
| [2013, 3, 1, 0, 0, 0, 1] |      0     |    0   |    2    | 27b8 5c96 0020 0002 |
+--------------------------+------------+--------+---------+---------------------+
| [2013, 3, 1, 0, 0, 0, 1] |     15     |    1   |    0    | 27b8 5c96 003e 1000 |
+--------------------------+------------+--------+---------+---------------------+
| [2013, 3, 1, 0, 0, 0, 1] |     15     |    1   |    1    | 27b8 5c96 003e 1001 |
+--------------------------+------------+--------+---------+---------------------+
| [2013, 3, 1, 0, 0, 0, 1] |     15     |    1   |    2    | 27b8 5c96 003e 1002 |
+--------------------------+------------+--------+---------+---------------------+

As you can see, each Flake ID is 64 bits long, consisting of:

  • placeholder, a 1 bit placeholder to avoid issues in languages without unsigned integer data types.
  • timestamp, a 42 bit long number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.
  • datacenter, a 4 bit long datacenter identifier. It can take up to 16 unique values (including 0).
  • worker, a 5 bit long worker indentifier. It can take up to 32 unique values (including 0).
  • counter, a 12 bit long counter of ids in the same millisecond. It can take up to 4096 unique values.

Breakdown of bits for an id e.g. 27b8 5c96 003e 1001 (datacenter is 15 and worker 1, counter is 2) is as follows:

 0 010 0111 1011 1000 0101 1100 1001 0110 0000 0000 001 1 111 0 0001 0000 0000 0010
                                                                    |---- ---- ----|  12 bit counter
                                                             |- ----|                  5 bit worker
                                                       |- ---|                         4 bit datacenter
  |--- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---|                              42 bit timestamp
|-|                                                                                    1 bit reserved

Note that composition of datacenter id and worker id makes 512 unique generator identifiers. By modifying datacenter and worker id we can get up to 512 id generators on a single machine (e.g. each running in a separate process) or have 512 machines with a single id generator on each. It is also possible to provide a single 9 bit long identifier (up to 512 values). That id is internally split into datacenter (the most significant 4 bits) and worker (the least significant 5 bits).

Usage

Flake ID Generator returns 8 byte long node Buffer objects with its bytes representing 64 bit long id. Note that the number is stored in Big Endian format i.e. the most significant byte of the number is stored in the smallest address given and the least significant byte is stored in the largest.

var FlakeId63 = require('flake-idgen-63');
var flakeIdGen63 = new FlakeId63();

console.log(flakeIdGen63.next());
console.log(flakeIdGen63.next());
console.log(flakeIdGen63.next());

It would give something like:

<Buffer 27 b8 5c 96 00 20 00 00>
<Buffer 27 b8 5c 96 00 20 00 01>
<Buffer 27 b8 5c 96 00 20 00 02>

Formatting

var biguintformat = require('biguint-format');
const FlakeId63 = require('flake-idgen-63')

var flakeIdGen63 = new FlakeId63();

console.info(intformat(flakeIdGen63.next(), 'dec'));
console.info(intformat(flakeIdGen63.next(), 'hex', { groupsize: 2 }));
console.info(intformat(flakeIdGen63.next(), 'bin', { groupsize: 4 }));

It would give something like:

2862139362510897152 // decimal
27 b8 5c 96 00 20 00 01  // hex
0010 0111 1011 1000 0101 1100 1001 0110 0000 0000 0010 0000 0000 0000 0000 0010  // binary