Skip to content

Instantly share code, notes, and snippets.

@yano3nora
Last active April 4, 2024 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yano3nora/ac151a51b8d63bf6c1a5dd15cc5f0df3 to your computer and use it in GitHub Desktop.
Save yano3nora/ac151a51b8d63bf6c1a5dd15cc5f0df3 to your computer and use it in GitHub Desktop.
[js: cuid] Collision-resistant ids optimized for horizontal scaling and performance. #js

Overview

paralleldrive/cuid2
一意な識別子の生成でUUID/ULID/CUID/Nano IDなど検討してみた

  • 時系列ソート可能な timestamp 依存の unique id を生成するやつ
  • uuid のような random より sortable がありがたいケースではこっち
  • js (node) だけでなく、様々なランタイムで実装されている
  • cuid1 は deprecated になったので cuid2 の方を利用する
$ npm i @paralleldrive/cuid2
import { createId } from '@paralleldrive/cuid2'

const ids = [
  createId(), // 'tz4a98xxat96iws9zmbrgj3a'
  createId(), // 'pfh0haxfpzowht3oi213cqos'
  createId(), // 'nc6bzmkmd014706rfda898to'
]

export const cuid = createId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment