PHPackages                             thecrypticace/lazy - 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. thecrypticace/lazy

ActiveLibrary

thecrypticace/lazy
==================

Collection-like wrapper around Iterators

v1.1.0(8y ago)6252MITPHPPHP &gt;=7.0.0

Since Mar 17Pushed 7y ago1 watchersCompare

[ Source](https://github.com/thecrypticace/lazy)[ Packagist](https://packagist.org/packages/thecrypticace/lazy)[ RSS](/packages/thecrypticace-lazy/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (8)Used By (0)

Lazy Collections
================

[](#lazy-collections)

[![Build Status](https://camo.githubusercontent.com/2fd8ad297bc1d22f89d6c5827970191ff2d1535377a49ef5072c5a1707af27b8/68747470733a2f2f7472617669732d63692e6f72672f746865637279707469636163652f6c617a792e737667)](https://travis-ci.org/thecrypticace/lazy)[![Coverage Status](https://camo.githubusercontent.com/03fcf5faf2f7b6dbef0c65e8a68b75bafa9e05ec1036826d8634a0eb42a808ba/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f746865637279707469636163652f6c617a792f6d61737465722e737667)](https://codecov.io/github/thecrypticace/lazy?branch=master)[![Total Downloads](https://camo.githubusercontent.com/ff39e9b7a8b7fcf0abff62779736d9bbcdc70ed7a502f993f7de16ae62335d04/68747470733a2f2f706f7365722e707567782e6f72672f746865637279707469636163652f6c617a792f642f746f74616c2e737667)](https://packagist.org/packages/thecrypticace/lazy)[![Latest Stable Version](https://camo.githubusercontent.com/450887f2b0319515d440b568ac3a8e27b951776799a3a70a45b77b7dca5a3294/68747470733a2f2f706f7365722e707567782e6f72672f746865637279707469636163652f6c617a792f762f737461626c652e737667)](https://packagist.org/packages/thecrypticace/lazy)[![License](https://camo.githubusercontent.com/bc0d58425b10019c043a6de81bc69c20dbcbbe64deb36c0925fd19bf19a6405b/68747470733a2f2f706f7365722e707567782e6f72672f746865637279707469636163652f6c617a792f6c6963656e73652e737667)](https://packagist.org/packages/thecrypticace/lazy)

What
----

[](#what)

Lazy is a Collection-like wrapper that can operate on iterators one item at a time (on-demand).

Some notes:

1. Lazy works well for one-shot data sources (e.g. `Generator`s)
2. Lazy supports algorithms which run with minimal overhead. Functional algorithms are well suited to this.
3. Lazy collections can convert the underlying data source into an array using `->eager()`. Useful for reiterative purposes.
4. Has convenience methods for simple lazy data generation (e.g. ranges)
5. Has higher-order collection proxy which will allow code like this: `$collection->map->people->map->count()->sum()`

Why?
----

[](#why)

Let's say you have this code:

```
collect($oneMillionNumbers)->filter(function ($n) {
    return $n % 2 === 0;
})->map(function ($n) {
    return $n / 4;
})->filter(function ($n) {
    return $n >= 100;
})
->first()
```

For a small array `$items` the work performed here is trivial. If you take a large array containing 1 million numbers starting at one the work performed is large:

- 1,000,000 iterations in `filter`
- 500,000 iterations in `map`
- 500,000 iterations in `filter`
- one iteration in `first`

Total: 2,000,001 iterations Total: 2,000,001 function calls

Each one of these iterations also incurs the overhead of a function call.

If you replace that call to `collect` with `lazy`:

```
lazy($oneMillionNumbers)->filter(function ($n) {
    return $n % 2 === 0;
})->map(function ($n) {
    return $n / 4;
})->filter(function ($n) {
    return $n >= 100;
})
->first()
```

These are the stats:

- 400 iterations in `filter`
- 200 iterations in `map`
- 200 iterations in `filter`
- one iteration in `first`

Since these iterations happen on demand it's not 801 iterations. It's 400 for the entire set. *Lazy* Collections perform the minimal amount of work needed to get the result.

There *are* 801 function calls. One for each of the "virtual" iterations in each of the operations. Still, this number is far less than the 2,000,001 calls from the loop above.

Total: 400 iterations Total: 801 function calls

*Lazy* is effectively performing the same operation as this:

```
foreach ($oneMillionNumbers as $n) {
    if ($n % 2 === 0) {
        $n = $n / 4;
        if ($n >= 100) {
            return $n;
        }
    }
}
```

tip: `lazy_range(1, 1000000)` will generate a Collection with a range from one to one million.

###  Health Score

30

—

LowBetter than 65% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 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 ~107 days

Recently: every ~139 days

Total

7

Last Release

2697d ago

Major Versions

1.0.x-dev → 2.0.x-dev2018-12-21

PHP version history (2 changes)v1.0.0PHP &gt;=7.0.0

2.0.x-devPHP &gt;=7.2

### Community

Maintainers

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

---

Top Contributors

[![thecrypticace](https://avatars.githubusercontent.com/u/614993?v=4)](https://github.com/thecrypticace "thecrypticace (72 commits)")

---

Tags

collectionsiterationlazywrappercollectionsiteratorslazy

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/thecrypticace-lazy/health.svg)

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

###  Alternatives

[doctrine/collections

PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.

6.0k411.1M1.2k](/packages/doctrine-collections)[cakephp/collection

Work easily with arrays and iterators by having a battery of utility traversal methods

899.9M10](/packages/cakephp-collection)[dusank/knapsack

Collection library for PHP

5351.0M25](/packages/dusank-knapsack)[phootwork/collection

The phootwork library fills gaps in the php language and provides better solutions than the existing ones php offers.

3924.8M15](/packages/phootwork-collection)[rybakit/twig-deferred-extension

An extension for Twig that allows to defer block rendering.

1092.7M3](/packages/rybakit-twig-deferred-extension)[cerbero/lazy-json

Framework-agnostic package to load JSONs of any dimension and from any source into Laravel lazy collections.

254309.8k1](/packages/cerbero-lazy-json)

PHPackages © 2026

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