Convert Figma logo to code with AI

ulid logojavascript

Universally Unique Lexicographically Sortable Identifier

3,122
108
3,122
3

Top Related Projects

Universally Unique Lexicographically Sortable Identifier

25,336

A tiny (124 bytes), secure, URL-friendly, unique string ID generator for JavaScript

Quick Overview

The ulid/javascript repository provides an implementation of the Universally Unique Lexicographically Sortable Identifier (ULID) specification in JavaScript. ULIDs are designed to be a replacement for traditional UUID v4 identifiers, offering improved sortability and time-based ordering.

Pros

  • Sortable: ULIDs are lexicographically sortable, which can be useful for applications that require efficient storage and retrieval of identifiers.
  • Time-based: ULIDs include a timestamp component, allowing for easy identification of the creation time of an identifier.
  • Compact: ULIDs are 26 characters long, which is shorter than the 36 characters of a UUID v4.
  • Widely Adopted: The ULID specification is gaining popularity and is supported by various programming languages, making it a viable alternative to UUIDs.

Cons

  • Compatibility: While ULIDs are designed to be a replacement for UUIDs, not all systems and libraries may be compatible with the ULID format.
  • Timestamp Precision: The timestamp component of a ULID has a precision of only 100 nanoseconds, which may not be sufficient for some high-precision use cases.
  • Potential Collisions: While the probability of ULID collisions is low, it is still possible, especially in high-throughput systems.
  • Lack of Widespread Adoption: While the ULID specification is gaining traction, it is not yet as widely adopted as the UUID standard.

Code Examples

import { ulid } from 'ulid';

// Generate a new ULID
const newUlid = ulid();
console.log(newUlid); // Output: 01GKBR4EPTSV4R1Q667MRCBKH3

// Parse an existing ULID
const parsedUlid = ulid.deconstruct(newUlid);
console.log(parsedUlid);
/*
Output:
{
  timestamp: 1681302420000,
  randomness: '4EPTSV4R1Q667MRCBKH3'
}
*/

// Generate a ULID with a custom timestamp
const customTimestamp = Date.now();
const customUlid = ulid(customTimestamp);
console.log(customUlid);

In the first example, we generate a new ULID using the ulid() function. The resulting ULID is a 26-character string.

The second example demonstrates how to parse an existing ULID using the ulid.deconstruct() function, which returns an object with the timestamp and randomness components.

The third example shows how to generate a ULID with a custom timestamp, which can be useful for certain use cases.

Getting Started

To use the ulid/javascript library in your project, you can install it using npm or yarn:

npm install ulid

or

yarn add ulid

Once installed, you can import the ulid function and use it in your code:

import { ulid } from 'ulid';

const newUlid = ulid();
console.log(newUlid); // Output: 01GKBR4EPTSV4R1Q667MRCBKH3

The library provides a simple API for generating, parsing, and working with ULIDs. You can refer to the project's README for more detailed usage examples and documentation.

Competitor Comparisons

Universally Unique Lexicographically Sortable Identifier

Pros of javascript

  • Identical functionality and API
  • Same implementation and performance characteristics
  • Consistent documentation and usage examples

Cons of javascript

  • No unique advantages over the other repository
  • Potential confusion for users due to duplicate repositories
  • Lack of differentiation in features or improvements

Code Comparison

Both repositories share the same codebase, so there are no differences to highlight. Here's a sample of the ULID generation function from both:

function ulid(seedTime) {
  if (seedTime == null) {
    seedTime = Date.now();
  }
  return encodeTime(seedTime, 10) + encodeRandom(16);
}

Summary

The javascript and javascript repositories are identical in every aspect, including code, functionality, and documentation. This unusual situation might lead to confusion among users and developers. It's unclear why two separate repositories with the same content exist. Users should be aware that choosing either repository will yield the same results and experience. For the most up-to-date information and potential future divergence, it's advisable to check both repositories periodically.

25,336

A tiny (124 bytes), secure, URL-friendly, unique string ID generator for JavaScript

Pros of nanoid

  • Smaller size: nanoid is more lightweight and has a smaller footprint
  • Faster generation: nanoid can generate IDs more quickly
  • Customizable alphabet: Allows for flexible ID generation with custom characters

Cons of nanoid

  • Less information: Unlike ULID, nanoid doesn't encode timestamp information
  • Not sortable: nanoid IDs are not inherently sortable by creation time
  • Potential for collisions: Higher chance of collisions compared to ULID in certain scenarios

Code Comparison

nanoid:

import { nanoid } from 'nanoid';
const id = nanoid(); // => "V1StGXR8_Z5jdHi6B-myT"

ULID:

import { ulid } from 'ulid';
const id = ulid(); // => "01ARZ3NDEKTSV4RRFFQ69G5FAV"

Both libraries offer simple, one-line ID generation. However, ULID provides timestamp-encoded, sortable IDs, while nanoid focuses on compact, fast, and customizable ID generation. The choice between them depends on specific project requirements, such as the need for time-based sorting or smaller ID sizes.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README



ulid


Universally Unique Lexicographically Sortable Identifier

Tests codecov npm npm

ULIDs are unique, sortable identifiers that work much in the same way as UUIDs, though with some improvements:

  • Lexicographically sortable
  • Canonically encoded as a 26 character string, as opposed to the 36 character UUID
  • Uses Crockford's base32 for better efficiency and readability (5 bits per character)
  • Monotonic sort order (correctly detects and handles the same millisecond)

ULIDs also provide:

  • 128-bit compatibility with UUID
  • 1.21e+24 unique IDs per millisecond
  • Case insensitivity
  • No special characters (URL safe)

UUID can be suboptimal for many uses-cases because:

  • It isn't the most character efficient way of encoding 128 bits of randomness
  • UUID v1/v2 is impractical in many environments, as it requires access to a unique, stable MAC address
  • UUID v3/v5 requires a unique seed and produces randomly distributed IDs, which can cause fragmentation in many data structures
  • UUID v4 provides no other information than randomness which can cause fragmentation in many data structures

Installation

Install using NPM:

npm install ulid --save

Compatibility

ULID supports the following environments:

VersionNodeJSBrowsersReact-NativeWeb WorkersEdge Functions
v3v18+YesYesYes?
v2v16+YesNoNoNo

Additionally, both ESM and CommonJS entrypoints are provided.

Usage

To quickly generate a ULID, you can simply import the ulid function:

import { ulid } from "ulid";

ulid(); // "01ARZ3NDEKTSV4RRFFQ69G5FAV"

Seed Time

You can also input a seed time which will consistently give you the same string for the time component. This is useful for migrating to ulid.

ulid(1469918176385) // "01ARYZ6S41TSV4RRFFQ69G5FAV"

Monotonic ULIDs

To generate monotonically increasing ULIDs, create a monotonic counter with monotonicFactory.

Note that the same seed time is being passed in for this example to demonstrate its behaviour when generating multiple ULIDs within the same millisecond

import { monotonicFactory } from "ulid";

const ulid = monotonicFactory();

// Strict ordering for the same timestamp, by incrementing the least-significant random bit by 1
ulid(150000); // "000XAL6S41ACTAV9WEVGEMMVR8"
ulid(150000); // "000XAL6S41ACTAV9WEVGEMMVR9"
ulid(150000); // "000XAL6S41ACTAV9WEVGEMMVRA"
ulid(150000); // "000XAL6S41ACTAV9WEVGEMMVRB"
ulid(150000); // "000XAL6S41ACTAV9WEVGEMMVRC"

// Even if a lower timestamp is passed (or generated), it will preserve sort order
ulid(100000); // "000XAL6S41ACTAV9WEVGEMMVRD"

Pseudo-Random Number Generators

ulid automatically detects a suitable (cryptographically-secure) PRNG. In the browser it will use crypto.getRandomValues and on NodeJS it will use crypto.randomBytes.

Using Math.random (insecure)

By default, ulid will not use Math.random to generate random values. You can bypass this limitation by overriding the PRNG:

const ulid = monotonicFactory(() => Math.random());

ulid(); // "01BXAVRG61YJ5YSBRM51702F6M"

Validity

You can verify if a value is a valid ULID by using isValid:

import { isValid } from "ulid";

isValid("01ARYZ6S41TSV4RRFFQ69G5FAV"); // true
isValid("01ARYZ6S41TSV4RRFFQ69G5FA"); // false

ULID Time

You can encode and decode ULID timestamps by using encodeTime and decodeTime respectively:

import { decodeTime } from "ulid";

decodeTime("01ARYZ6S41TSV4RRFFQ69G5FAV"); // 1469918176385

Note that while decodeTime works on full ULIDs, encodeTime encodes only the time portion of ULIDs:

import { encodeTime } from "ulid";

encodeTime(1469918176385); // "01ARYZ6S41"

Tests

Install dependencies using npm install first, and then simply run npm test to run the test suite.

CLI

ulid can be used on the command line, either via global install:

npm install -g ulid
ulid

Or via npx:

npx ulid

You can also generate multiple IDs at the same time:

ulid --count 15

Specification

You can find the full specification, as well as information regarding implementations in other languages, over at ulid/spec.

Performance

You can test ulid's performance by running npm run bench:

Simple ulid x 56,782 ops/sec ±2.50% (86 runs sampled)
ulid with timestamp x 58,574 ops/sec ±1.80% (87 runs sampled)
Done!

NPM DownloadsLast 30 Days