What is yeast?
The yeast npm package is a simple utility for generating unique IDs based on the current time. It is lightweight and designed to be used in environments where unique identifiers are needed, such as in distributed systems or for generating unique keys in databases.
What are yeast's main functionalities?
Generate Unique IDs
This feature allows you to generate a unique ID based on the current time. The generated ID is a string that can be used as a unique identifier in various applications.
const yeast = require('yeast');
const uniqueID = yeast();
console.log(uniqueID);
Other packages similar to yeast
uuid
The uuid package is a popular library for generating universally unique identifiers (UUIDs). Unlike yeast, which generates IDs based on the current time, uuid generates IDs that are globally unique and can be used in a wide range of applications. It supports multiple versions of UUIDs, including time-based, random, and namespace-based UUIDs.
shortid
The shortid package is another library for generating short, unique, non-sequential IDs. It is similar to yeast in that it generates short IDs, but it uses a different algorithm that ensures the IDs are unique even across distributed systems. shortid is useful for cases where you need a human-readable ID that is still unique.
nanoid
The nanoid package is a modern library for generating unique IDs with a focus on performance and security. It generates shorter IDs than uuid and is more secure than shortid. nanoid is suitable for use in both client-side and server-side applications and provides a good balance between ID length and uniqueness.
yeast
Yeast is a unique id generator. It has been primarily designed to generate a
unique id which can be used for cache busting. A common practice for this is
to use a timestamp, but there are couple of downsides when using timestamps.
- The timestamp is already 13 chars long. This might not matter for 1 request
but if you make hundreds of them this quickly adds up in bandwidth and
processing time.
- It's not unique enough. If you generate two stamps right after each other,
they would be identical because the timing accuracy is limited to
milliseconds.
Yeast solves both of these issues by:
- Compressing the generated timestamp using a custom
encode()
function that
returns a string representation of the number. - Seeding the id in case of collision (when the id is identical to the previous
one).
To keep the strings unique it will use the .
char to separate the generated
stamp from the seed.
Installation
The module is intended to be used in browsers as well as in Node.js and is
therefore released in the npm registry and can be installed using:
npm install --save yeast
Usage
All the examples assume that this library is initialized as follow:
'use strict';
var yeast = require('yeast');
To generate an id just call the yeast
function.
console.log(yeast(), yeast(), yeast());
setTimeout(function () {
console.log(yeast());
});
yeast.encode(num)
An helper function that returns a string representing the specified number. The
returned string contains only URL safe characters.
yeast.encode(+new Date());
yeast.decode(str)
An helper function that returns the integer value specified by the given string.
This function can be used to retrieve the timestamp from a yeast
id.
var id = yeast();
yeast.decode(id);
That's all folks. If you have ideas on how we can further compress the ids
please open an issue!
License
MIT