A high-performance ULID (Universally Unique Lexicographically Sortable Identifier) generator using WebAssembly, up to 40x faster than traditional implementations, optimized for backend use.
![icon](https://private-user-images.githubusercontent.com/2261067/328056656-4b708bac-c20f-44c8-a2ec-9f11ad4d2e60.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ0MTcxMTEsIm5iZiI6MTcyNDQxNjgxMSwicGF0aCI6Ii8yMjYxMDY3LzMyODA1NjY1Ni00YjcwOGJhYy1jMjBmLTQ0YzgtYTJlYy05ZjExYWQ0ZDJlNjAucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyMyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjNUMTI0MDExWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MTM5NTkyNmRlNTBiNWJlNjBjNDM4YWFhNzI5Y2VmM2Y1YmY2NDU1NzhmYzM4YmY3ZjlhY2E4YzkyN2YyZmQwOSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.Q-hhAIjWpLG50-9AUbolqByHhJcNAhX7ezFkp-cDhH4)
- High Performance: Leveraging WebAssembly for significantly faster performance in generating ULIDs.
- Secure Randomness: Utilizes WebAssembly's cryptographic methods, not requiring external crypto modules.
- Easy Integration: Fully compatible with ulid's API.
Comparisons with ulid
- Approximately 40x faster in main ULID generation methods.
- Maintains all API signatures for seamless migration.
- Additional initialization step required for WebAssembly module.
Install using npm:
npm install wa-ulid
First, initialize the WebAssembly module:
import init, { ulid } from "wa-ulid";
await init();
Then, generate ULIDs:
console.log(ulid());
The ulid
function accepts an optional seed time to generate a consistent time-based component:
console.log(ulid(1593045370000));
For generating monotonically increasing identifiers:
const ulid = monotonicFactory();
console.log(ulid());
console.log(ulid());
While detectPrng
is maintained for API compatibility, the random number generation is handled internally by the WebAssembly module and does not utilize the browser or Node.js crypto APIs.
This feature allows you to specify your own pseudo-random number generator if needed:
import { factory } from "wa-ulid";
import customPrng from "./myCustomPrng";
const ulid = factory(customPrng);
console.log(ulid());
- The WebAssembly module increases the file size to approximately 110KB, compared to 4.7KB for ulid.
- Recommended primarily for backend environments due to the larger file size, though it is fully functional in browsers.
The benchmarks below demonstrate the significant performance advantage over ulid.
Function | ulid | wa-ulid | Performance Increase |
---|---|---|---|
encodeTime |
3,863,741 ops/sec | 4,904,495 ops/sec | 1.27x |
decodeTime |
1,951,730 ops/sec | 5,336,862 ops/sec | 2.73x |
ulid |
44,758 ops/sec | 1,933,859 ops/sec | 43.20x |
monotonicUlid() |
2,382,881 ops/sec | 3,505,757 ops/sec | 1.47x |
These tests were run on an Apple M2 Pro processor, highlighting the efficiency of the WASM-based implementation. You can run benchmarks in your own environment using pnpm benchmark
.
This project is licensed under the MIT License. See LICENSE for details.