shortid vs node-uuid vs uuid vs uuidv4
ユニークID生成ライブラリ
shortidnode-uuiduuiduuidv4類似パッケージ:

ユニークID生成ライブラリ

ユニークID生成ライブラリは、アプリケーション内で一意の識別子を生成するためのツールです。これらのライブラリは、データベースのレコード、セッションID、トランザクションIDなど、さまざまな用途で使用されます。ユニークIDは、衝突を避けるために重要であり、特に分散システムやクラウドベースのアプリケーションでのデータの整合性を保つために必要です。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
shortid948,3785,72421.7 kB161年前MIT
node-uuid0268-09年前-
uuid015,23266.7 kB36ヶ月前MIT
uuidv40-17.4 kB--MIT

機能比較: shortid vs node-uuid vs uuid vs uuidv4

生成するIDの形式

  • shortid:

    shortidは、短くて可読性の高いIDを生成します。生成されるIDは、通常7〜14文字で、URLに適した形式です。

  • node-uuid:

    node-uuidは、RFC 4122に準拠したUUIDを生成します。これにより、UUIDのバージョン1(タイムスタンプベース)やバージョン4(ランダム生成)をサポートしています。

  • uuid:

    uuidは、UUIDを生成するための強力なライブラリで、バージョン1、3、4、5をサポートしています。特に、バージョン1はタイムスタンプに基づいており、バージョン4はランダム生成です。

  • uuidv4:

    uuidv4は、UUIDのバージョン4を生成します。完全にランダムなIDを生成し、衝突の可能性が非常に低いです。

依存関係

  • shortid:

    shortidは、軽量で依存関係が少なく、特に小規模なプロジェクトやサーバーレスアプリケーションに適しています。

  • node-uuid:

    node-uuidは、外部の依存関係が少なく、シンプルな設計で、Node.js環境での使用に最適です。

  • uuid:

    uuidは、Node.jsとブラウザの両方で使用でき、広範な互換性を持っています。

  • uuidv4:

    uuidv4は、非常に軽量で、依存関係がなく、Node.js環境での使用に特化しています。

パフォーマンス

  • shortid:

    shortidは、短いIDを迅速に生成するために最適化されており、パフォーマンスが高いです。特に、短いIDが必要な場合に効果的です。

  • node-uuid:

    node-uuidは、UUIDを生成するための効率的なアルゴリズムを使用しており、パフォーマンスが高いです。特に大量のUUIDを生成する必要がある場合に適しています。

  • uuid:

    uuidは、UUIDの生成において非常に効率的で、特にバージョン4の生成が迅速です。

  • uuidv4:

    uuidv4は、UUIDのバージョン4を迅速に生成するために設計されており、パフォーマンスが高いです。

ユースケース

  • shortid:

    shortidは、URLの短縮や一時的なセッションIDの生成など、短いIDが必要なシナリオに最適です。

  • node-uuid:

    node-uuidは、データベースの主キーやトランザクションIDなど、ユニークな識別子が必要なシナリオに適しています。

  • uuid:

    uuidは、広範なユースケースに対応でき、特に分散システムやクラウドアプリケーションでの使用に適しています。

  • uuidv4:

    uuidv4は、ランダムなユニークIDが必要なシナリオ、特にセッション管理や一時的な識別子に最適です。

メンテナンスとサポート

  • shortid:

    shortidは、軽量でシンプルなため、メンテナンスが容易です。

  • node-uuid:

    node-uuidは、長い間使用されており、安定したメンテナンスが行われています。

  • uuid:

    uuidは、広く使用されており、活発なコミュニティによるサポートがあります。

  • uuidv4:

    uuidv4は、シンプルで軽量なため、メンテナンスが容易で、特に小規模なプロジェクトに適しています。

選び方: shortid vs node-uuid vs uuid vs uuidv4

  • shortid:

    shortidは、短くてユニークなIDを生成するためのライブラリです。短いIDが必要で、可読性やURLの短縮が重要な場合に適しています。

  • node-uuid:

    node-uuidは、UUID(ユニバーサルユニークID)を生成するための標準的なライブラリです。UUIDのバージョン4を使用する場合や、RFC 4122に準拠したUUIDが必要な場合に選択してください。

  • uuid:

    uuidは、UUIDを生成するためのシンプルで強力なライブラリです。特にUUIDのバージョン1(タイムスタンプベース)やバージョン4(ランダム生成)が必要な場合に選択してください。

  • uuidv4:

    uuidv4は、UUIDのバージョン4を生成するための軽量なライブラリです。シンプルで使いやすく、特にランダムなユニークIDが必要な場合に最適です。

shortid のREADME

shortid Build Status shortid

Amazingly short non-sequential url-friendly unique id generator.

shortid is deprecated, because the architecture is unsafe. we instead recommend Nano ID, which has the advantage of also being significantly faster than shortid

ShortId creates amazingly short non-sequential url-friendly unique ids. Perfect for url shorteners, MongoDB and Redis ids, and any other id users might see.

  • By default 7-14 url-friendly characters: A-Z, a-z, 0-9, _-
  • Supports cluster (automatically), custom seeds, custom alphabet.
  • Can generate any number of ids without duplicates, even millions per day.
  • Perfect for games, especially if you are concerned about cheating so you don't want an easily guessable id.
  • Apps can be restarted any number of times without any chance of repeating an id.
  • Popular replacement for Mongo ID/Mongoose ID.
  • Works in Node, io.js, and web browsers.
  • Includes Mocha tests.

ShortId does not generate cryptographically secure ids, so don't rely on it to make IDs which are impossible to guess.

Usage

const shortid = require('shortid');

console.log(shortid.generate());
// PPBqWA9

Mongoose Unique Id

_id: {
  'type': String,
  'default': shortid.generate
},

Browser Compatibility

The best way to use shortid in the browser is via browserify or webpack.

These tools will automatically only include the files necessary for browser compatibility.

All tests will run in the browser as well:

## build the bundle, then open Mocha in a browser to see the tests run.
$ grunt build open

Example

~/projects/shortid ❯ node examples/examples.js
eWRhpRV
23TplPdS
46Juzcyx
dBvJIh-H
2WEKaVNO
7oet_d9Z
dogPzIz8
nYrnfYEv
a4vhAoFG
hwX6aOr7

Real World Examples

shortId was created for Node Knockout 2011 winner for Most Fun Doodle Or Die. Millions of doodles have been saved with shortId filenames. Every log message gets a shortId to make it easy for us to look up later.

Here are some other projects that use shortId:

  • bevy - A simple server to manage multiple Node services.
  • capre - Cross-Server Data Replication.
  • cordova-build - an alternative to phonegap build that runs on your servers/agents.
  • couchdb-tools - A library of handy functions for use when working with CouchDB documents.
  • CleverStack/clever-email - E-mail system for CleverStack.
  • CloudTypes - JavaScript end2end implementation of the Cloud Types model for Eventual Consistency programming.
  • dnode-tarantula - an asynchronous rpc and event system for node.js based on dnode-protocol and TCP sockets.
  • mongoose-url-shortener - A simple URL Shortening library for NodeJS using Promises/A+ results.
  • mozilla/smokejumper - The Smoke Jumper project is an effort to bring dead simple, secure, P2P file sharing to Firefox.
  • shortness - Node based URL shortener that uses SQLite.
  • file-db - Document database that uses directories and files to store its data, supporting nested key-value objects in named collections.
  • resume-generator - Resume Generator.
  • riffmint - Collaboration in musical space.
  • rap1ds/dippa - Dippa Editor – A web-based LaTeX editor

API

var shortid = require('shortid');

shortid.generate()

Returns string non-sequential unique id.

Example

users.insert({
  _id: shortid.generate(),
  name: '...',
  email: '...'
});

shortid.characters(string)

Default: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'

Returns new alphabet as a string

Recommendation: If you don't like _ or -, you can to set new characters to use.

Optional

Change the characters used.

You must provide a string of all 64 unique characters. Order is not important.

The default characters provided were selected because they are url safe.

Example

// use $ and @ instead of - and _
shortid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@');
// any 64 unicode characters work, but I wouldn't recommend this.
shortid.characters('ⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ①②③④⑤⑥⑦⑧⑨⑩⑪⑫');

shortid.isValid(id)

Returns boolean

Check to see if an id is a valid shortid. Note: This only means the id could have been generated by shortid, it doesn't guarantee it.

Example

shortid.isValid('41XTDbE');
// true
shortid.isValid('i have spaces');
// false

shortid.worker(integer)

Default: process.env.NODE_UNIQUE_ID || 0

Recommendation: You typically won't want to change this.

Optional

If you are running multiple server processes then you should make sure every one has a unique worker id. Should be an integer between 0 and 16. If you do not do this there is very little chance of two servers generating the same id, but it is theoretically possible if both are generated in the exact same second and are generating the same number of ids that second and a half-dozen random numbers are all exactly the same.

Example

shortid.worker(1);

shortid.seed(integer)

Default: 1

Recommendation: You typically won't want to change this.

Optional

Choose a unique value that will seed the random number generator so users won't be able to figure out the pattern of the unique ids. Call it just once in your application before using shortId and always use the same value in your application.

Most developers won't need to use this, it's mainly for testing ShortId.

If you are worried about users somehow decrypting the id then use it as a secret value for increased encryption.

Example

shortid.seed(1000);

About the Author

Hi! Thanks for checking out this project! My name is Dylan Greene. When not overwhelmed with my two young kids I enjoy contributing to the open source community. I'm also a tech lead at Opower. @dylang @dylang

Here's some of my other Node projects:

NameDescriptionnpm Downloads
npm‑checkCheck for outdated, incorrect, and unused dependencies.npm-check
grunt‑notifyAutomatic desktop notifications for Grunt errors and warnings. Supports OS X, Windows, Linux.grunt-notify
space‑hogsDiscover surprisingly large directories from the command line.space-hogs
rssRSS feed generator. Add RSS feeds to any project. Supports enclosures and GeoRSS.rss
grunt‑promptInteractive prompt for your Grunt config using console checkboxes, text input with filtering, password fields.grunt-prompt
xmlFast and simple xml generator. Supports attributes, CDATA, etc. Includes tests and examples.xml
changelogCommand line tool (and Node module) that generates a changelog in color output, markdown, or json for modules in npmjs.org's registry as well as any public github.com repo.changelog
grunt‑attentionDisplay attention-grabbing messages in the terminalgrunt-attention
observatoryBeautiful UI for showing tasks running on the command line.observatory
anthologyModule information and stats for any @npmjs useranthology
grunt‑catEcho a file to the terminal. Works with text, figlets, ascii art, and full-color ansi.grunt-cat

This list was generated using anthology.

License

Copyright (c) 2016 Dylan Greene, contributors.

Released under the MIT license.

Screenshots are CC BY-SA (Attribution-ShareAlike).