Download the PHP package kofan/deferred-collection without Composer
On this page you can find all versions of the php package kofan/deferred-collection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kofan/deferred-collection
More information about kofan/deferred-collection
Files in kofan/deferred-collection
Package deferred-collection
Short Description PHP collections with deferred operations and execution
License MIT
Informations about the package deferred-collection
Deferred Collection - PHP collections with deferred data processing
Introduction and motivation
The main purpose of this library is to defer execution of different operations on collection items until your application really needs it. It turns out that very often the reason of application poor performance is an execution of unneeded operations on the collection of items. Moreover, performing heavy operations on the whole collection at once is usually very inefficient and takes too much memory. So, for example, let's take a look at the code
So there are several problems with this code there.
- First of all, it tries to read all the models to the process memory at once. So if there are 1000 records found then it will try to read all 1000 into the process memory.
- Second of all, it could be that this query is not needed at all if
somethingWrongHappened()
because in that case, an error is going to be thrown. So in this situation, the database query and its results are the waste of resources. - And, of course, during the data rendering it might happen that we won't need to iterate through all queries at all, even though we have fetched them from the database.
Thus, Deferred Collections are aiming to solve this problem. All the operations are actually going to be performed only when the collection is started being iterated through. For example: