You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Does it make sense that ulid() is so slow relative to monothonic?
I ran this simple benchmark multiple times to test this library vs uuidv7, and it always returns around the same results:
import { ulid, monotonicFactory } from 'ulid';
function bench(name, fn, N = 5_000_000) {
// warmup
for (let i = 0; i < 200_000; i++) fn();
const t0 = performance.now();
for (let i = 0; i < N; i++) fn();
const t1 = performance.now();
console.log(`${name}: ${(N / ((t1 - t0) / 1000)).toFixed(1)} ids/sec`);
}
bench('uuidv7', uuidv7);
bench('ulid', ulid);
bench('ulid(monotonic)', monotonicFactory());
Does it make sense that ulid() is so slow relative to monothonic? I ran this simple benchmark multiple times to test this library vs uuidv7, and it always returns around the same results:
import { ulid, monotonicFactory } from 'ulid';
function bench(name, fn, N = 5_000_000) {
// warmup
for (let i = 0; i < 200_000; i++) fn();
const t0 = performance.now();
for (let i = 0; i < N; i++) fn();
const t1 = performance.now();
console.log(`${name}: ${(N / ((t1 - t0) / 1000)).toFixed(1)} ids/sec`);
}
bench('uuidv7', uuidv7);
bench('ulid', ulid);
bench('ulid(monotonic)', monotonicFactory());
Does it make sense that ulid() is so slow relative to monothonic? I ran this simple benchmark multiple times to test this library vs uuidv7, and it always returns around the same results:
import { ulid, monotonicFactory } from 'ulid';
function bench(name, fn, N = 5_000_000) {
// warmup
for (let i = 0; i < 200_000; i++) fn();
const t0 = performance.now();
for (let i = 0; i < N; i++) fn();
const t1 = performance.now();
console.log(`${name}: ${(N / ((t1 - t0) / 1000)).toFixed(1)} ids/sec`);
}
bench('uuidv7', uuidv7);
bench('ulid', ulid);
bench('ulid(monotonic)', monotonicFactory());
Activity
cmcnicholas commentedon Sep 12, 2025
Looks like some work is done on every call to
ulidindetectPRNG()and seed creation (tiny but it is repeated).A way to improve that without needing the factory is if they allowed us to call
detectPRNG()so we could store it once. e.g.this avoids the repeated testing of
detectPRNG.I think there might be room for improvement though, I can see allocating the Uint32Array each time a call is made is also pretty expensive: https://github.com/ulid/javascript/blob/master/source/ulid.ts#L51
Suggestion would be they move the array initialisation to the outer part of the function and reuse it.
If you know your environment its running in you can just add this today e.g.
Anyway, just some thoughts.
cmcnicholas commentedon Sep 13, 2025
just opened a PR after I went on a bit of a dive through the code #127 makes
ulidwork about 45% faster.