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

ms-flakeless

v4.7.2

Published

Generates unique IDs from timestamps

Downloads

90

Readme

Flakeless

Flakeless is a Javascript library and Node.js native addon for computing small, unique IDs for web applications. This is useful when incrementing primary keys are either not available (ex: in Cassandra or Neo4j) or not feasible at your current scale.

How it Works

The strategy for generating IDs is identical to how Twitter generates IDs with Snowflake. Flakeless is an implementation of their algorithm for Node.js. The primary benefit of Flakeless over the other Javascript implementations is that Flakeless was written as a native addon, so it has the ease of use of any other Javascript library but has the performance of C++.

The way Snowflake (and by proxy, Flakeless) works is by combining the following values:

  • The time difference between the current time and the starting epoch in milliseconds. This accounts for the most significant 41 bits.

  • A number signifying the computer generating the ID. This is the middle 10 bits, and is entirely arbitrary and is assigned by the user.

  • A counter denoting how many other IDs have been generated in this millisecond. This accounts for the least significant 12 bits.

Installing

Before you install Flakeless, you first need to make sure that node-gyp is installed. Because Flakeless is written primarily in C++, it first needs to be compiled.

npm install -g node-gyp

Once node-gyp is installed, install Flakeless.

npm install ms-flakeless

Usage

Initialization:

const Flakeless = require('ms-flakeless');
const flakeless = new Flakeless({
  epochStart: Date.now(),
  outputType: 'base64',
  workerID: 71
});

epochStart is a number representing the start of the time period in milliseconds used to generate IDs. IDs will potentially begin repeating approximately 69 years after this date. This defaults to the beginning of the Unix epoch.

outputType is a string representing which alphabet to use when returning the ID. The options are base10 (using characters 0-9), base16 (using characters 0-9 and A-F), and base64 (using characters -, 0-9, A-Z, _, and a-z). Regardless of which format is specified, the output will always be a string. This defaults to base64.

workerID is an arbitrary identifier signifying the computer generating the IDs. This allows multiple computers to generate IDs without every needing to communicate state. The range for this is from 0 to 1023, and it defaults to 0 if not provided.

Generating an ID:

let id = flakeless.next();

Flakeless#next takes no arguments, and returns a string containing the next unique ID. Strings were used because Flakeless generates 63-bit IDs, and Javascript only supports up to 52-bit integers before precision declines.

Using the built-in Client and Server:

const Flakeless = require('flakeless').Flakeless;
const Client = require('flakeless').Client;
const Server = require('flakeless').Server;

const flakeless = new Flakeless;
const server = new Server(6000, function() {
  server.addGenerator('/a', flakeless);
});

const client = new Client('http://localhost:6000/a')
client.next().then(function(ids) {
  // use the IDs here
});

Performance

Preliminary benchmarks yield the following results:

| Flakeless | Flakeless JS | node-snowflake | Date.now() | |------------------|------------------|------------------|------------------| | 0.000099 ms/call | 0.003470 ms/call | 0.003870 ms/call | 0.000069 ms/call | | 10101 calls/ms | 288 calls/ms | 258 calls/ms | 14492 calls/ms |

* All tests done by measuring the average time per call over 10,000,000 calls while outputting to base64, except for node-snowflake which only outputs decimal values and Date.now() which was used as a reference.

Roadmap

  • Soon Flakeless will support providing custom times to Flakeless#next instead of using the current system clock in milliseconds.
  • Eventually, Flakeless will support custom output formats that deviate from the provided 41-10-12 format, and will allow for the utilization of MAC addresses.

Contributing

If you think you can add useful features to Flakeless or can make it faster, please contribute! Most of the work here was done in a handful of days while learning how to use V8 and NAN, so there's definitely a lot of room for improvement.