ULID vs UUID: Sortable Random ID Generators for JavaScript

What’s the right tool for generating universal identifiers?

UUID is one of the most used universal identifiers in software development. However, new alternatives have challenged its existence over the past few years.

Out of these, ULID is one of the leading competitors since it provides sortable unique IDs.

In this article, I will discuss the features of ULID with examples to give you a better understanding of when to use it.

Understanding ULID and its Usage

ULID stands for Universally Unique Alphabetically Sortable Identifier.

It has more than 271K weekly NPM downloads and 1.7K GitHub Stars.

Usage

You can easily install ULID NPM Library using npm i ulid command and use it in your project.

import { ulid } from ‘ulid’;
ulid();

It comes with some amazing features that resolve some of the drawbacks of UUID. For example, when using UUID with relational databases, there can be difficulties indexing the data due to the lack of inbuilt ordering. In such situations, you might be forced to include another attribute to make data sortable.

Besides, UUID has some common issues regarding its randomness, efficiency, and seeding, addressed by ULID. Therefore, let’s look at ULID in detail.

1. Use Both Timestamp and Randomness

When you generate an ID using UUID, it will generate a 36 character long string by only considering randomness or timestamp.

But, ULID considers both randomness and timestamp to generate IDs and they are encoded as 26 character strings (128 bits).

// Example UUID
01FHZXHK8PTP9FVK99Z66GXQTX

The first 10 characters of the ULID represent the timestamp and the second part of the ULID represents randomness. Both these parts are base 32 encoded strings and represented using 48 bits and 80 bits, respectively.

For example, break down of the above ULID would look like this:

01FHZXHK8PTP9FVK99Z66GXQTX
Timestamp (48 bits) - 01FHZXHK8P
Randomness (80 bits) - TP9FVK99Z66GXQTX

Note: ULIDs are encoded using Crockford’s Base32 alphabet (0123456789ABCDEFGHJKMNPQRSTVWXYZ). It excludes I, L, O, and U letters to avoid any unexpected confusion.

2. UILDs are Lexicographically Sortable

Lexicographical sortability is one of the most highlighted features of ULID.

As we are already aware, ULIDs can be sorted. This feature of ULID allows developers to easily manage database-related tasks like sorting, partitioning, and indexing.

For example, you don’t need to create an extra column to maintain the record created time. Instead, you can use the timestamp representation of ULID to order or partition data based on the created time.

Note: The timestamp part of the ULID is represented with UNIX-time in milliseconds, and it won’t run out of space ’til the year 10889 AD.

3. High Security with Random Numbers

Most random ID generators use unsafeMath.random() to generate IDs. But, ULID blocks the use of Math.random() by default and automatically decides a suitable random number generator based on the situation.

For example, it will use crypto.getRandomValues for browsers and crypto.randomBytes for Node environments.

However, if you want to use Math.random() in ULID, you need to allow permission for that explicitly.

import { factory, detectPrng } from 'ulid'

const random_number_gen = detectPrng(true)
const ulid = factory(random_number_gen)

Note: You can also use your own pseudo-random number generators to generate ULIDs.

4. Monotonic ULIDs & Seed Time

ULID allows you to get IDs with the same timestamp by passing a seed time. For example, if you want to create IDs with 2021–10–15 as the timestamp, you need to pass the UNIX timestamp in milliseconds to ulid() function.

ulid(1634263671000) // 01FJ0V986RA01G70YQ5Z0AMQE7

In addition to that, ULID allows to create series of IDs with continuously increasing values. All you need to do is create a ulid object using monotonicFactory and pass the same time seed.

import { monotonicFactory } from ‘ulid’
const ulid = monotonicFactory()
console.log(ulid(100000)); // 00000031N0J7R2B57M8YG73J7M
console.log(ulid(100000)); // 00000031N0J7R2B57M8YG73J7N
console.log(ulid(100000)); // 00000031N0J7R2B57M8YG73J7P
console.log(ulid(100000)); // 00000031N0J7R2B57M8YG73J7Q
console.log(ulid(100000)); // 00000031N0J7R2B57M8YG73J7R

5. Multi-Language Support

ULID supports nearly 50 languages, including JavaScript, Java, C++, Dart, Python, and .NET.

Also, binary representations are available for more than 15 languages, including C++, Dart, Go, JavaScript and Python.

6. JavaScript Module Support

ULID can be easily used with all types of JavaScript modules, including ES6+, CommonJS, and AMD.

// TypeScript , ES6+ Modules
import { ulid } from ‘ulid’;
ulid();
// CommonJS
const ULID = require('ulid');
ULID.ulid();
// AMD
define(['ULID'] , function (ULID) {
ULID.ulid()
});
// Browser
<script src="https://unpkg.com/ulid@2.3.0/dist/index.umd.js"></script>
<script>
ULID.ulid()
</script>

Other Features

  • Can generate 1.21e+24 unique ULIDs per millisecond.
  • ULIDs are URL safe since it does not use any special characters.
  • Small bundle size — 2.5 kB (minified), 1.2kB (GZipped).
  • Download time is around 1ms –10 ms.
  • Shorter than UUIDs.
  • Compatible with UUIDs 128 format.

Future Focus

Based on many expert opinions in StackOverflow, there are no significant disadvantages or limitations of using ULID.

However, case insensitivity and the 80-bit randomness are the main disadvantages developers notice in ULID. But its lexicographically sorting ability makes it unique among all the others.

Also, if we consider the usage trend of ULID over the past year, we can see that it is on an upward trend. Although the download count is much smaller than UUID, it has gained more than 150000 users within the past year.

https://www.npmtrends.com/ulid

With all these features and my experience using UUID and ULID, it’s a no-brainer for use cases that demand sorting. So, don’t hesitate to use ULID in your next project and share your thought with others in the comment section.

Thank you for Reading !!!

Build better Component Libs and Design Systems

Share components across teams and projects to speed up development and make sure your users experience a consistent design at every touchpoint.

Bit offers a great dev experience for building, sharing, and adopting components across teams and applications. Create a component hub for free give it a try →

An independently source-controlled and shared “card” component. On the right => its dependency graph, auto-generated by Bit.

Learn More


ULID vs UUID: Sortable Random ID Generators for JavaScript was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Chameera Dulanga

What’s the right tool for generating universal identifiers?

UUID is one of the most used universal identifiers in software development. However, new alternatives have challenged its existence over the past few years.

Out of these, ULID is one of the leading competitors since it provides sortable unique IDs.

In this article, I will discuss the features of ULID with examples to give you a better understanding of when to use it.

Understanding ULID and its Usage

ULID stands for Universally Unique Alphabetically Sortable Identifier.

It has more than 271K weekly NPM downloads and 1.7K GitHub Stars.

Usage

You can easily install ULID NPM Library using npm i ulid command and use it in your project.

import { ulid } from ‘ulid’;
ulid();

It comes with some amazing features that resolve some of the drawbacks of UUID. For example, when using UUID with relational databases, there can be difficulties indexing the data due to the lack of inbuilt ordering. In such situations, you might be forced to include another attribute to make data sortable.

Besides, UUID has some common issues regarding its randomness, efficiency, and seeding, addressed by ULID. Therefore, let’s look at ULID in detail.

1. Use Both Timestamp and Randomness

When you generate an ID using UUID, it will generate a 36 character long string by only considering randomness or timestamp.

But, ULID considers both randomness and timestamp to generate IDs and they are encoded as 26 character strings (128 bits).

// Example UUID
01FHZXHK8PTP9FVK99Z66GXQTX

The first 10 characters of the ULID represent the timestamp and the second part of the ULID represents randomness. Both these parts are base 32 encoded strings and represented using 48 bits and 80 bits, respectively.

For example, break down of the above ULID would look like this:

01FHZXHK8PTP9FVK99Z66GXQTX
Timestamp (48 bits) - 01FHZXHK8P
Randomness (80 bits) - TP9FVK99Z66GXQTX
Note: ULIDs are encoded using Crockford’s Base32 alphabet (0123456789ABCDEFGHJKMNPQRSTVWXYZ). It excludes I, L, O, and U letters to avoid any unexpected confusion.

2. UILDs are Lexicographically Sortable

Lexicographical sortability is one of the most highlighted features of ULID.

As we are already aware, ULIDs can be sorted. This feature of ULID allows developers to easily manage database-related tasks like sorting, partitioning, and indexing.

For example, you don’t need to create an extra column to maintain the record created time. Instead, you can use the timestamp representation of ULID to order or partition data based on the created time.

Note: The timestamp part of the ULID is represented with UNIX-time in milliseconds, and it won’t run out of space ’til the year 10889 AD.

3. High Security with Random Numbers

Most random ID generators use unsafeMath.random() to generate IDs. But, ULID blocks the use of Math.random() by default and automatically decides a suitable random number generator based on the situation.

For example, it will use crypto.getRandomValues for browsers and crypto.randomBytes for Node environments.

However, if you want to use Math.random() in ULID, you need to allow permission for that explicitly.

import { factory, detectPrng } from 'ulid'

const random_number_gen = detectPrng(true)
const ulid = factory(random_number_gen)
Note: You can also use your own pseudo-random number generators to generate ULIDs.

4. Monotonic ULIDs & Seed Time

ULID allows you to get IDs with the same timestamp by passing a seed time. For example, if you want to create IDs with 2021–10–15 as the timestamp, you need to pass the UNIX timestamp in milliseconds to ulid() function.

ulid(1634263671000) // 01FJ0V986RA01G70YQ5Z0AMQE7

In addition to that, ULID allows to create series of IDs with continuously increasing values. All you need to do is create a ulid object using monotonicFactory and pass the same time seed.

import { monotonicFactory } from ‘ulid’
const ulid = monotonicFactory()
console.log(ulid(100000)); // 00000031N0J7R2B57M8YG73J7M
console.log(ulid(100000)); // 00000031N0J7R2B57M8YG73J7N
console.log(ulid(100000)); // 00000031N0J7R2B57M8YG73J7P
console.log(ulid(100000)); // 00000031N0J7R2B57M8YG73J7Q
console.log(ulid(100000)); // 00000031N0J7R2B57M8YG73J7R

5. Multi-Language Support

ULID supports nearly 50 languages, including JavaScript, Java, C++, Dart, Python, and .NET.

Also, binary representations are available for more than 15 languages, including C++, Dart, Go, JavaScript and Python.

6. JavaScript Module Support

ULID can be easily used with all types of JavaScript modules, including ES6+, CommonJS, and AMD.

// TypeScript , ES6+ Modules
import { ulid } from ‘ulid’;
ulid();
// CommonJS
const ULID = require('ulid');
ULID.ulid();
// AMD
define(['ULID'] , function (ULID) {
ULID.ulid()
});
// Browser
<script src="https://unpkg.com/ulid@2.3.0/dist/index.umd.js"></script>
<script>
ULID.ulid()
</script>

Other Features

  • Can generate 1.21e+24 unique ULIDs per millisecond.
  • ULIDs are URL safe since it does not use any special characters.
  • Small bundle size — 2.5 kB (minified), 1.2kB (GZipped).
  • Download time is around 1ms –10 ms.
  • Shorter than UUIDs.
  • Compatible with UUIDs 128 format.

Future Focus

Based on many expert opinions in StackOverflow, there are no significant disadvantages or limitations of using ULID.

However, case insensitivity and the 80-bit randomness are the main disadvantages developers notice in ULID. But its lexicographically sorting ability makes it unique among all the others.

Also, if we consider the usage trend of ULID over the past year, we can see that it is on an upward trend. Although the download count is much smaller than UUID, it has gained more than 150000 users within the past year.

https://www.npmtrends.com/ulid

With all these features and my experience using UUID and ULID, it’s a no-brainer for use cases that demand sorting. So, don’t hesitate to use ULID in your next project and share your thought with others in the comment section.

Thank you for Reading !!!

Build better Component Libs and Design Systems

Share components across teams and projects to speed up development and make sure your users experience a consistent design at every touchpoint.

Bit offers a great dev experience for building, sharing, and adopting components across teams and applications. Create a component hub for free give it a try →

An independently source-controlled and shared “card” component. On the right => its dependency graph, auto-generated by Bit.

Learn More


ULID vs UUID: Sortable Random ID Generators for JavaScript was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Chameera Dulanga


Print Share Comment Cite Upload Translate Updates
APA

Chameera Dulanga | Sciencx (2021-10-21T15:54:10+00:00) ULID vs UUID: Sortable Random ID Generators for JavaScript. Retrieved from https://www.scien.cx/2021/10/21/ulid-vs-uuid-sortable-random-id-generators-for-javascript/

MLA
" » ULID vs UUID: Sortable Random ID Generators for JavaScript." Chameera Dulanga | Sciencx - Thursday October 21, 2021, https://www.scien.cx/2021/10/21/ulid-vs-uuid-sortable-random-id-generators-for-javascript/
HARVARD
Chameera Dulanga | Sciencx Thursday October 21, 2021 » ULID vs UUID: Sortable Random ID Generators for JavaScript., viewed ,<https://www.scien.cx/2021/10/21/ulid-vs-uuid-sortable-random-id-generators-for-javascript/>
VANCOUVER
Chameera Dulanga | Sciencx - » ULID vs UUID: Sortable Random ID Generators for JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/21/ulid-vs-uuid-sortable-random-id-generators-for-javascript/
CHICAGO
" » ULID vs UUID: Sortable Random ID Generators for JavaScript." Chameera Dulanga | Sciencx - Accessed . https://www.scien.cx/2021/10/21/ulid-vs-uuid-sortable-random-id-generators-for-javascript/
IEEE
" » ULID vs UUID: Sortable Random ID Generators for JavaScript." Chameera Dulanga | Sciencx [Online]. Available: https://www.scien.cx/2021/10/21/ulid-vs-uuid-sortable-random-id-generators-for-javascript/. [Accessed: ]
rf:citation
» ULID vs UUID: Sortable Random ID Generators for JavaScript | Chameera Dulanga | Sciencx | https://www.scien.cx/2021/10/21/ulid-vs-uuid-sortable-random-id-generators-for-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.