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

@theinternetfolks/snowflake

v1.3.0

Published

Library to help you create a Snowflake Id or parse the same.

Downloads

4,989

Readme

The Internet Folks Logo

@theinternetfolks/snowflake

GitHub license Maintainer Downloads Npm package version

Library to help you create a Snowflake Id or parse the same. This solves the problem of generating unique identifiers at scale.

What are Snowflakes?

Snowflake IDs, or snowflakes, are a form of unique identifier used in distributed computing. The format was created by Twitter and is used for the IDs of tweets. The format has been adopted by other companies, including Discord, and Instagram, which uses a modified version.

By default, the ID format follows the original Twitter snowflake format.

  • The ID as a whole is a 63 bit integer stored in a bigint
  • 41 bits are used to store a timestamp with millisecond precision, using a custom epoch.
  • 10 bits are used to store a node id - a range from 0 through 1023.
  • 12 bits are used to store a sequence number - a range from 0 through 4095.

Why Snowflakes are better than random UUIDs?

Snowflakes are sortable by time, because they are based on the time they were created. Additionally, the time a snowflake was created can be calculated from the snowflake. This can be used to get snowflakes (and their associated objects) that were created before or after a particular date.

Who uses Snowflake?

  • Twitter uses snowflake IDs for tweets, direct messages, users, lists, and all other objects available over the API.
  • Discord also uses snowflakes, with their epoch set to the first second of the year 2015.
  • Instagram uses a modified version of the format, with 41 bits for a timestamp, 13 bits for a shard ID, and 10 bits for a sequence number.

How it Works

Each time you generate an ID, it works, like this.

  • A timestamp with millisecond precision is stored using 41 bits of the ID.
  • Then the NodeID is added in subsequent bits.
  • Then the Sequence Number is added, starting at 0 and incrementing for each ID generated in the same millisecond. If you generate enough IDs in the same millisecond that the sequence would roll over or overfill then the generate function will pause until the next millisecond.

The default Twitter format shown below.

+--------------------------------------------------------------------------+
| 1 Bit Unused | 41 Bit Timestamp |  10 Bit NodeID  |   12 Bit Sequence ID |
+--------------------------------------------------------------------------+

Performance

With default settings, this snowflake generator should be sufficiently fast enough on most systems to generate 4096 unique ID's per millisecond. This is the maximum that the snowflake ID format supports. That is, around 243-244 nanoseconds per operation.

Since the snowflake generator is single threaded the primary limitation will be the maximum speed of a single processor on your system.

Installation

Install with npm

  npm install @theinternetfolks/snowflake

Install with yarn

  yarn add @theinternetfolks/snowflake

Usage

Simple Generation

import { Snowflake } from "@theinternetfolks/snowflake";

console.log(Snowflake.generate());
// 6917062538869867520

Advanced Generation

import { Snowflake } from "@theinternetfolks/snowflake";

console.log(Snowflake.generate({ timestamp: 1649156222074 }));
// 6917062538869867520
import { Snowflake } from "@theinternetfolks/snowflake";

console.log(Snowflake.generate({ timestamp: 1649157035498, shard_id: 4 }));
// 6917065950617407488

API

static generate(
    {
        timestamp?: Date | number;
        shard_id?: number;
        epoch?: number;
    }
): string;
static parse(snowflake: string | number | bigint): {
    timestamp: number;
    shard_id: number;
    binary: string;
};

Test Coverage

License

MIT

The project is based on a fork off of (snowflake-generator)[https://github.com/FatAussieFatBoy/snowflake-generator].