日本語のREADMEはこちらです: README.ja.md
This project is an implementation of the ULID specification.
UUID can be suboptimal for many use-cases because it isn't efficiently sortable. ULID offers a compelling alternative:
- 128-bit compatibility with UUID
- 1.21e+24 unique ULIDs per millisecond
- Lexicographically sortable for efficient database indexing
- Canonically encoded as a 26-character string, as opposed to the 36-character UUID
- URL-safe with no special characters
- Case-insensitive and uses Crockford's Base32 for readability and efficiency
- Monotonic sort order to ensure ordering even for ULIDs generated in the same millisecond
npm install --save ulidimport { ulid } from "https://unpkg.com/ulid@2.3.0/dist/index.esm.js";
console.log(ulid());<!-- Get the latest version from https://unpkg.com/ulid/dist/ -->
<script src="https://unpkg.com/ulid@2.3.0/dist/index.umd.js"></script>
<script>
// The global variable is `ULID`.
console.log(ULID.ulid());
</script>Import and call ulid() to generate a new identifier.
ES Modules
import { ulid } from 'ulid'
ulid() // 01ARZ3NDEKTSV4RRFFQ69G5FAVCommonJS
const { ulid } = require('ulid')
ulid() // 01ARZ3NDEKTSV4RRFFQ69G5FAVYou can generate a ULID for a specific millisecond timestamp by passing it as an argument. This is useful for migrating data or for generating ULIDs that correspond to a past event.
import { ulid } from 'ulid'
ulid(1469918176385) // 01ARYZ6S41TSV4RRFFQ69G5FAVTo guarantee that ULIDs generated within the same millisecond are strictly ordered, use the monotonicFactory. This is useful in high-frequency scenarios where multiple ULIDs could be created in the same millisecond.
The factory returns a ulid function that will increment the random part of the ULID if it's called in the same millisecond as the previous call.
import { monotonicFactory } from 'ulid'
const ulid = monotonicFactory()
// The following ULIDs are generated in the same millisecond
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVR8
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVR9
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVRA
// Even if a lower timestamp is passed, it will preserve sort order by
// incrementing the previous ULID.
ulid(100000) // 000XAL6S41ACTAV9WEVGEMMVRBBy default, ulid uses a cryptographically-secure PRNG (crypto.getRandomValues in browsers, crypto.randomBytes in Node.js). You can provide your own PRNG by using the factory function. Your PRNG function must return a random number between 0 and 1.
import { factory } from 'ulid'
import prng from 'your-random-number-generator'
const ulid = factory(prng)
ulid() // 01BXAVRG61YJ5YSBRM51702F6MThe monotonicFactory also accepts a PRNG as an argument.
import { monotonicFactory } from 'ulid'
import prng from 'your-random-number-generator'
const ulid = monotonicFactory(prng)
ulid() // 01BXAVRG61YJ5YSBRM51702F6MIn environments without a crypto module, ulid will throw an error. If security is not a concern, you can explicitly allow Math.random as a fallback.
import { factory, detectPrng } from 'ulid'
// Pass `true` to allow insecure fallback
const prng = detectPrng(true)
const ulid = factory(prng)
ulid()This library conforms to the official ulid/spec. You can find implementations in other languages there.
npm testnpm run perfulid
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
MIT
