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 🙏

© 2026 – Pkg Stats / Ryan Hefner

snowflake-id-utils

v1.0.5

Published

Distributed unique ID generator using the Snowflake algorithm. Support serverless like Cloudflare Workers

Readme

Snowflake ID Generator

A TypeScript utility for working with Snowflake IDs with Cloudflare Workers compatibility. This package provides a clean, type-safe way to parse, create, and deconstruct Snowflake IDs with zero dependencies.

Features

  • 🎯 Full TypeScript support
  • 🚀 Zero dependencies
  • 💪 Proper BigInt handling for ID operations
  • 🔒 Type-safe API
  • ✨ Cloudflare Worker Support
  • 📦 Small bundle size

Installation

npm install snowflake-id-utils

Quick Start

import { Snowflake } from "snowflake-id-utils";

// Create from an existing ID
const snowflake = new Snowflake("175928847299117063");

// Get creation timestamp
const timestamp = snowflake.getTimestamp();
console.log(timestamp); // -> Date object

// Get all components
const { timestamp, workerId, sequence } = snowflake.deconstruct();

API Reference

Creating Snowflakes

// From string ID
const snowflake = new Snowflake("175928847299117063");

// From timestamp, this sets the worker ID to 0
const fromTime = Snowflake.fromTimestamp(new Date());

// From timestamp, this sets the worker ID to 123
const fromTimeWithWorker = Snowflake.fromTimestamp(new Date(), 123);

// From environment variable
const fromEnv = Snowflake.fromEnv("ENV_VAR");

// Parse from string (with null handling)
const parsed = Snowflake.parse("175928847299117063");

Instance Methods

const snowflake = new Snowflake("175928847299117063");

// Get creation timestamp
const timestamp = snowflake.getTimestamp();

// Get worker ID (0-4095)
const workerId = snowflake.getWorkerId();

// Get sequence number (0-1023)
const sequence = snowflake.getSequence();

// Get all components at once
const deconstructed = snowflake.deconstruct();

// Convert to string
const str = snowflake.toString();

// JSON serialization is handled automatically
const json = JSON.stringify(snowflake);

Types

interface DeconstructedSnowflake {
    timestamp: Date;
    workerId: number;
    processId: number;
    sequence: number;
}

Examples

Working with Snowflake IDs

import { Snowflake } from "snowflake-id-utils";

// Create from ID
const id = new Snowflake("175928847299117063");

// Get creation time
const createdAt = id.getTimestamp();
console.log(`ID was created at: ${createdAt}`);

// Check if ID was created before a certain date
const isOld = createdAt < new Date("2024-01-01");

Creating New Snowflakes

import { Snowflake } from "snowflake-id-utils";

// Create a snowflake for the current timestamp
const now = Snowflake.fromTimestamp(Date.now());

// Create a snowflake for a specific date
const specific = Snowflake.fromTimestamp(new Date("2023-01-01T00:00:00Z"));

Environment Variables

import { Snowflake } from "snowflake-id-utils";

// process.env.ENV_VAR = '175928847299117063'
const id = Snowflake.fromEnv("ENV_VAR");

if (id) {
    console.log(`ID created at: ${id.getTimestamp()}`);
} else {
    console.log("ID not found in environment");
}

Technical Details

Snowflake Structure

This implementation of Snowflake ID is a 63-bit integer with the following structure:

111111111111111111111111111111111111111111 11111111111 1111111111
63                                         21          10         0
  • Timestamp (42 bits): Milliseconds since Epoch (2023-01-01)
    • Provides ~139 years of unique timestamps
  • Worker ID (11 bits): Worker ID (0-2047)
    • Supports 2,048 unique workers
  • Sequence (10 bits): Sequence number (0-1023)
    • Allows 1,024 unique IDs per millisecond per worker

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT