Convert Figma logo to code with AI

ulid logojavascript

Universally Unique Lexicographically Sortable Identifier

2,980
106
2,980
31

Top Related Projects

Universally Unique Lexicographically Sortable Identifier

24,296

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.

24,296

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


Build Status codecov npm

Universally Unique Lexicographically Sortable Identifier

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

Instead, herein is proposed ULID:

  • 128-bit compatibility with UUID
  • 1.21e+24 unique ULIDs per millisecond
  • 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)
  • Case insensitive
  • No special characters (URL safe)
  • Monotonic sort order (correctly detects and handles the same millisecond)

Install with a script tag

<script src="https://unpkg.com/ulid@{{VERSION_NUMBER}}/dist/index.umd.js"></script>
<script>
    ULID.ulid()
</script>

Install with NPM

npm install --save ulid

Import

TypeScript, ES6+, Babel, Webpack, Rollup, etc.. environments

import { ulid } from 'ulid'

ulid() // 01ARZ3NDEKTSV4RRFFQ69G5FAV

CommonJS environments

const ULID = require('ulid')

ULID.ulid()

AMD (RequireJS) environments

define(['ULID'] , function (ULID) {
  ULID.ulid()
});

Usage

To generate a ULID, simply run the 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.

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 node it will use crypto.randomBytes.

Allowing the insecure Math.random

By default, ulid will not use Math.random, because that is insecure. To allow the use of Math.random, you'll have to use factory and detectPrng.

import { factory, detectPrng } from 'ulid'

const prng = detectPrng(true) // pass `true` to allow insecure
const ulid = factory(prng)

ulid() // 01BXAVRG61YJ5YSBRM51702F6M

Use your own PRNG

To use your own pseudo-random number generator, import the factory, and pass it your generator function.

import { factory } from 'ulid'
import prng from 'somewhere'

const ulid = factory(prng)

ulid() // 01BXAVRG61YJ5YSBRM51702F6M

You can also pass in a prng to the monotonicFactory function.

import { monotonicFactory } from 'ulid'
import prng from 'somewhere'

const ulid = monotonicFactory(prng)

ulid() // 01BXAVRG61YJ5YSBRM51702F6M

Implementations in other languages

Refer to ulid/spec

Specification

Refer to ulid/spec

Test Suite

npm test

Performance

npm run perf
ulid
336,331,131 op/s » encodeTime
102,041,736 op/s » encodeRandom
17,408 op/s » generate


Suites:  1
Benches: 3
Elapsed: 7,285.75 ms

NPM DownloadsLast 30 Days