PHPackages                             mediagone/small-uid - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mediagone/small-uid

ActiveLibrary[Database &amp; ORM](/categories/database)

mediagone/small-uid
===================

Small Unique Identifier - Quite like an ULID, but half smaller (64 bits).

0.7.2(1mo ago)2073612MITPHP

Since Feb 20Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/Mediagone/small-uid)[ Packagist](https://packagist.org/packages/mediagone/small-uid)[ RSS](/packages/mediagone-small-uid/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (6)Versions (13)Used By (2)

Small UID
=========

[](#small-uid)

⚠️ *This project is in experimental phase, the API may may be subject to change.*

[![Latest Version on Packagist](https://camo.githubusercontent.com/41c8b11c6fc697a503bf765b9f41533bfccafb93564e1c24fd42efc0e2b182b5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d65646961676f6e652f736d616c6c2d7569642e737667)](https://packagist.org/packages/mediagone/small-uid)[![Total Downloads](https://camo.githubusercontent.com/912fdf0908353729c88fbf96a47ba1d2d5a52832a505192af1728c74a5634bc8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d65646961676f6e652f736d616c6c2d7569642e737667)](https://packagist.org/packages/mediagone/small-uid)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)

UUIDs are frequently used as database *Primary Key* in software development. However, they aren't the best choice mainly due to their random sorting and the resulting fragmentation in databases indexes.

Using [ULIDs](https://github.com/ulid/spec) is generally a very good alternative, solving most of UUID flaws.

**Small UIDs** are also an ideal alternative **when you do not need as much uniqueness** and want **shorter "user-friendly" encoded strings**.

Summary:

1. [Installation](#install)
2. Usages
    1. [Creation](#create)
    2. [Short-string representation](#shortstring)
    3. [Serialization](#serialize)
    4. [Retrieving creation DateTime](#datetime)
3. [License](#licence)

Introduction
------------

[](#introduction)

Small UIDs are short unique identifiers especially designed to be used as efficient database *Primary Key*:

- Half smaller than UUID / ULID (64-bit)
- Lexicographically sortable
- Encodable as a short user-friendly and URL-safe base-62 string (`a-zA-Z0-9`)
- User-friendly strings are generated in a way to be always very different (no shared prefix due to similar timestamps)

Small UIDULIDUUID v4Size64 bits128 bits128 bitsMonotonic sort orderYes \*\*YesNoRandom bits2080122Collision odds \*\*\*1,024 */ ms \**1.099e+12 */ ms \**2.305e+18\* *the Uid includes a timestamp, so collisions may occur only during the same millisecond.*
\*\* *monotonic sort order, but random order when generated at the same millisecond.*
\*\*\* *theorical number of generated Uids before the first expected collision.*

They are internally stored as *64-bit* integers (*44-bit* timestamp followed by *20 random bits*):

```
|-----------------------|  |------------|
        Timestamp            Randomness
         44 bits               20 bits

```

The random number suffix still guarantees a decent amount of uniqueness when many ids are created in the same millisecond (up to 1,048,576 different values) and you may only expect collision if you're generating more than 1024 random ids during the same millisecond.

### Sorting

[](#sorting)

Because of the sequential timestamp, *Small UIDs* are naturally sorted chronologically. It **improves indexing** when inserting values in databases, new ids being appended to the end of the table without reshuffling existing data (read more [in this article](https://www.codeproject.com/Articles/388157/GUIDs-as-fast-primary-keys-under-multiple-database)).

However, **sort order within the same millisecond is not guaranteed** because of the random bits suffix.

Installation
-----------------------------------------------

[](#installation)

This package requires **PHP (64-bit) 7.4+** and **GMP extension**.

Add it as Composer dependency:

```
$ composer require mediagone/small-uid
```

If you're using Doctrine ORM, you'll probably want to install also appropriate custom types:

```
$ composer require mediagone/small-uid-doctrine
```

Usages
------

[](#usages)

### Creation

[](#creation)

Most useful way to generate an Uid is the `random()` static factory method, which creates a new random Uid using the current timestamp and a random suffix:

```
$uid = SmallUid::random();
```

You can also generate an Uid from a 16-chars long hexadecimal string:

```
$uid = SmallUid::fromHex('1234567890abcdef');
```

You can also generate an Uid from a base62-encoded string using `fromString` method, but this will detailed in the next section.

```
$uid = SmallUid::fromString('LscmjzUyKLR');
```

In some cases, you may need a *null-object* Uid, therefore there is a special static factory method:

```
$uid = SmallUid::nil();
```

### Short-string representation

[](#short-string-representation)

Using the hexadecimal representation is not always convenient (eg. for use in URLs), hopefully it can be safely converted back and forth to a base-62 string, which is only 10 or 11-chars long (depending on the Uid's internal value).

To get an uid's short-string representation, just cast it to a string:

```
$uid = SmallUid::fromHex('1234567890abcdef');
(string)$uid; // string(11) "LscmjzUyKLR"
```

To convert back the short-string to an Uid instance, use the `fromString()` static factory method:

```
$uid = SmallUid::fromString('LscmjzUyKLR');
```

### Serialization

[](#serialization)

You serialize your Uids using their hexadecimal representation (which is always a 16-chars long hexadecimal string):

```
$uid = SmallUid::fromString('LscmjzUyKLR');
(string)$uid->getHex(); // string(16) "1234567890abcdef"
```

Uids can also be converted to an 8-bit integer or a binary string (*eg. for database persistence*):

```
$uid = SmallUid::fromString('LscmjzUyKLR');

(string)$uid->toHex()->toDecimal(); // int(1311768467294899695)
(string)$uid->toHex()->toBinary(); // string(8) "4Vx" (binary string)
```

### Retrieving creation DateTime

[](#retrieving-creation-datetime)

Every Uid embeds a timestamp reflecting its creation datetime:

```
$uid = SmallUid::fromHex('1234567890abcdef');

$datetime = $uid->getCreationDatetime(); // retrieve Uid's creation datetime
(string)$creationDatetime; // "2009-08-23T03:58:16+00:00"
```

License
------------------------------------------

[](#license)

*Small UID* is licensed under MIT license. See LICENSE file.

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance89

Actively maintained with recent releases

Popularity25

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~169 days

Recently: every ~427 days

Total

12

Last Release

53d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ce000f43f69a7dc38a80da57d86a712a3e27f86b5fd4090f024472a03b28a089?d=identicon)[Mediagone](/maintainers/Mediagone)

---

Top Contributors

[![Mediagone](https://avatars.githubusercontent.com/u/32240357?v=4)](https://github.com/Mediagone "Mediagone (34 commits)")

---

Tags

identifieruliduuiddatabaseuuidguidentityUIDulidprimary key

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mediagone-small-uid/health.svg)

```
[![Health](https://phpackages.com/badges/mediagone-small-uid/health.svg)](https://phpackages.com/packages/mediagone-small-uid)
```

###  Alternatives

[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[robmorgan/phinx

Phinx makes it ridiculously easy to manage the database migrations for your PHP app.

4.5k46.2M405](/packages/robmorgan-phinx)[vlucas/spot2

Simple DataMapper built on top of Doctrine DBAL

605392.8k7](/packages/vlucas-spot2)[rorecek/laravel-ulid

Laravel package for ULID (Universally Unique Lexicographically Sortable Identifier)

52160.3k1](/packages/rorecek-laravel-ulid)[ramsey/identifier

A PHP library for generating and working with identifiers, including UUIDs, ULIDs, and Snowflakes

603.0k1](/packages/ramsey-identifier)[identifier/identifier

Common Interfaces and Factories for Identifiers

3226.2k1](/packages/identifier-identifier)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
