Download the PHP package ocramius/lazy-map without Composer
On this page you can find all versions of the php package ocramius/lazy-map. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ocramius/lazy-map
More information about ocramius/lazy-map
Files in ocramius/lazy-map
Package lazy-map
Short Description A library that provides lazy instantiation logic for a map of objects
License MIT
Homepage https://github.com/Ocramius/LazyMap
Informations about the package lazy-map
Lazy Map
This small library aims at providing a very simple and efficient map of lazy-instantiating objects.
Abandoned
Starting with PHP 8.3, dynamic properties are no longer allowed "out of the box".
While it is still possible to have dynamic properties via explicit declaration (the #[\AllowDynamicProperties]
attribute), the approach
of this package is no longer to be considered safe nor efficient long-term.
Based on that, this package is deprecated and abandoned: please use traditional PHP array
s instead.
Installation
The suggested installation method is via composer:
Usage
The current implementation is very simple and allows to define a map of "services" through a
LazyMap\CallbackLazyMap
:
Purpose
The idea behind the library is to avoid un-efficient lazy-loading operations like following:
This reduces overhead greatly when you'd otherwise call getSomething()
thousands of times.
That's especially useful when mapping a lot of different services and iterating over them
over and over again.
Performance
LazyMap actually performs much better than the "un-efficient" example that I've shown above. You can look directly at the performance test suite for details on the tested implementations, but here are some results for you to have an idea of the boost:
Initialized Map Performance:
Method Name | Ops/s | Relative |
---|---|---|
initializedArrayPerformance | 2,277,272.90002 | 100.00% |
initializedArrayMapPerformance | 1,536,988.76108 | 148.16% |
initializedLazyMapPerformance | 4,446,227.23514 | 51.22% |
Un-Initialized Map Performance:
Method Name | Ops/s | Relative |
---|---|---|
unInitializedArrayPerformance : | 1,091,720.80627 | 100.00% |
unInitializedArrayMapPerformance | 688,132.30083 | 158.65% |
unInitializedLazyMapPerformance: | 912,191.90744 | 119.68% |