PHPackages                             johnroyer/fastpriorityqueue - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. johnroyer/fastpriorityqueue

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

johnroyer/fastpriorityqueue
===========================

Efficient integer priority queue implementation in PHP

v1.14.0(6y ago)05BSD-3-ClausePHPPHP &gt;=7.2

Since Nov 30Pushed 6y ago1 watchersCompare

[ Source](https://github.com/johnroyer/FastPriorityQueue)[ Packagist](https://packagist.org/packages/johnroyer/fastpriorityqueue)[ Docs](https://github.com/ezimuel/fastpriorityqueue)[ RSS](/packages/johnroyer-fastpriorityqueue/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (3)Dependencies (2)Versions (8)Used By (0)

Fast Priority Queue implementation
----------------------------------

[](#fast-priority-queue-implementation)

[![Build Status](https://camo.githubusercontent.com/79102eba2e0925ffa885b109cdfdf84fccdfced3f78392295f2f5a28954b6b85/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f657a696d75656c2f466173745072696f7269747951756575652e7376673f6272616e63683d6d6173746572)](https://secure.travis-ci.org/ezimuel/FastPriorityQueue)

This is an efficient implementation of an **integer priority queue** in PHP. PHP offers the [SplPriorityQueue](http://php.net/manual/en/class.splpriorityqueue.php)class that implements a priority queue, but this component has a "strange" behaviour, see PHP request [\#60926](https://bugs.php.net/bug.php?id=60926)and PHP bug [\#53710](https://bugs.php.net/bug.php?id=53710). Basically, elements with the same priority are extracted in arbitrary order and this can be a problem in many situations. Even, though, the [definition](https://en.wikipedia.org/wiki/Priority_queue)of Priority Queue says:

> In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue.

Read the post [Taming SplPriorityQueue](https://mwop.net/blog/253-Taming-SplPriorityQueue.html)by [Matthew Weier O'Phinney](https://github.com/weierophinney) for more information about this `SplPriorityQueue` issue.

Implementation details
----------------------

[](#implementation-details)

I did not use the usual approach to implement the priority queue with a [heap](https://en.wikipedia.org/wiki/Heap_%28data_structure%29). I've used [ordered arrays](https://github.com/ezimuel/FastPriorityQueue/blob/master/src/PriorityQueue.php#L19)grouped by [priorities](https://github.com/ezimuel/FastPriorityQueue/blob/master/src/PriorityQueue.php#L26). For the array iteration I used the [next()](http://php.net/manual/en/function.next.php), and [current()](http://php.net/manual/en/function.current.php) functions of PHP.

To get the priorities in order, I use the [max()](http://php.net/manual/en/function.max.php)function of PHP. To retrieve the next priority, I remove the previous one from the array, using [unset()](http://php.net/manual/en/function.unset.php), and I re-apply the `max()` function to the remaining values.

This solution is very simple and offers very good performance (see the benchmark below).

Benchmark
---------

[](#benchmark)

I provided a simple script to test the performance of the implementation. You can execute this test running the following command:

```
php benchmark/test.php

```

This script executes 50'000 insert and extract operations using different priority queue implementations:

- [SplPriorityQueue](http://php.net/manual/en/class.splpriorityqueue.php)
- [Zend\\Stdlib\\SplPriorityQueue](https://github.com/zendframework/zend-stdlib/blob/master/src/SplPriorityQueue.php)
- [Zend\\Stdlib\\PriorityQueue](https://github.com/zendframework/zend-stdlib/blob/master/src/PriorityQueue.php)

I have also included the `SplPriorityQueue` of PHP to have a starter point for the comparison, even though the results of this component are not correct, as mentioned above.

I executed the benchmark using an Intel Core i5-2500 at 3.30GHz with 8 Gb of RAM running Ubuntu Linux 14.04 and PHP 5.5.9. Here the results:

```
--- Benchmark 50,000 insert and extract with a fixed priority
SplPriorityQueue                : 0.05173206 (sec)
FastPriorityQueue\PriorityQueue : 0.07072687 (sec)
Zend\Stdlib\SplPriorityQueue    : 0.23528290 (sec)
Zend\Stdlib\PriorityQueue       : 0.39357114 (sec)

--- Benchmark 50,000 insert and extract with random priority (1,100)
SplPriorityQueue                : 0.06713820 (sec)
FastPriorityQueue\PriorityQueue : 0.10090804 (sec)
Zend\Stdlib\SplPriorityQueue    : 0.44940901 (sec)
Zend\Stdlib\PriorityQueue       : 0.65850401 (sec)

```

As you can see the execution time of `FastPriorityQueue` is very good, about **3x to 6x faster** than the other implementations (except the `SplPriorityQueue` that is out of the comparison).

Unit Tests
----------

[](#unit-tests)

You can run the unit tests by executing the following commmand after the installation using [composer](https://getcomposer.org/).

```
vendor/bin/phpunit

```

Copyright
---------

[](#copyright)

This work is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/).

(C) Copyright 2015 by [Enrico Zimuel](http://www.zimuel.it).

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 50% 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 ~285 days

Recently: every ~0 days

Total

6

Last Release

2392d ago

PHP version history (2 changes)1.0.0PHP &gt;=5.5

v1.2PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/134a7921f61f2b847c6f81e18d0cea923d1b877e2d86ff4f4322a99c4642c147?d=identicon)[johnroyer](/maintainers/johnroyer)

---

Top Contributors

[![ezimuel](https://avatars.githubusercontent.com/u/475967?v=4)](https://github.com/ezimuel "ezimuel (12 commits)")[![EmanueleMinotto](https://avatars.githubusercontent.com/u/417201?v=4)](https://github.com/EmanueleMinotto "EmanueleMinotto (7 commits)")[![johnroyer](https://avatars.githubusercontent.com/u/114491?v=4)](https://github.com/johnroyer "johnroyer (4 commits)")[![vlakarados](https://avatars.githubusercontent.com/u/386678?v=4)](https://github.com/vlakarados "vlakarados (1 commits)")

---

Tags

splpriority queueSplPriorityQueue

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/johnroyer-fastpriorityqueue/health.svg)

```
[![Health](https://phpackages.com/badges/johnroyer-fastpriorityqueue/health.svg)](https://phpackages.com/packages/johnroyer-fastpriorityqueue)
```

###  Alternatives

[whichbrowser/parser

Useragent sniffing library for PHP

1.8k11.6M50](/packages/whichbrowser-parser)[ihor/nspl

Non-standard PHP library (NSPL) - functional primitives toolbox and more

381368.5k](/packages/ihor-nspl)[sdboyer/gliph

A graph library for PHP.

17029.1k1](/packages/sdboyer-gliph)[winter/wn-blog-plugin

Blog plugin for Winter CMS

2042.1k3](/packages/winter-wn-blog-plugin)

PHPackages © 2026

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