ocramius / lazy-map
A library that provides lazy instantiation logic for a map of objects
Fund package maintenance!
Ocramius
Installs: 477 412
Dependents: 3
Suggesters: 0
Security: 0
Stars: 87
Watchers: 4
Forks: 2
Open Issues: 8
Requires
- php: ~8.1.0 || ~8.2.0
Requires (Dev)
- doctrine/coding-standard: ^10.0.0
- phpbench/phpbench: ^1.2.7
- phpunit/phpunit: ^9.5.26
- roave/infection-static-analysis-plugin: ^1.26.0
- vimeo/psalm: ^5.0.0
- 2.9.x-dev
- 2.8.x-dev
- 2.8.0
- 2.7.x-dev
- 2.7.0
- 2.6.x-dev
- 2.6.0
- 2.5.x-dev
- 2.5.0
- 2.4.x-dev
- 2.4.0
- 2.3.x-dev
- 2.3.0
- 2.2.x-dev
- 2.2.0
- 2.1.0
- 2.0.0
- 1.0.0
- dev-renovate/phpunit-phpunit-10.x
- dev-renovate/all-minor-patch
- dev-renovate/actions-checkout-4.x
- dev-renovate/doctrine-coding-standard-12.x
- dev-renovate/lock-file-maintenance
- dev-dependabot/add-v2-config-file
- dev-feature/parametrised-factory-type
This package is auto-updated.
Last update: 2023-09-19 12:13:03 UTC
README
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:
composer require ocramius/lazy-map
Usage
The current implementation is very simple and allows to define a map of "services" through a
LazyMap\CallbackLazyMap
:
$map = new \LazyMap\CallbackLazyMap(function ($name) { $object = new \stdClass(); $object->name = $name; return $object; }); var_dump($map->foo); var_dump($map->bar); var_dump($map->{'something special'});
Purpose
The idea behind the library is to avoid un-efficient lazy-loading operations like following:
private function getSomething($name) { if (isset($this->initialized[$name]) || array_key_exists($name, $this->initialized)) { return $this->initialized[$name]; } return $this->initialized[$name] = new Something($name); }
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% |