This repository accompanies the article:
Lazy Evaluation in PHP: How Generators and Iterators Save Memory and Speed up Code
— dev.to: https://dev.to/oleksandr_vasyliev/lazy-evaluation-in-php-how-generators-and-iterators-save-memory-and-speed-up-code-3529
It demonstrates the difference in memory usage and runtime when processing large datasets with:
- classic arrays (eager loading)
- generators / iterators (lazy loading)
Author: Oleksandr Vasyliev
- PHP 8.2+ (works with 8.1+, but examples target 8.2)
- Composer (optional, for running PHPUnit tests)
# 1) Clone your fork, then:
cd php-lazy-evaluation-demo
# 2) Install dependencies
composer install
# 3) Generate sample CSV & NDJSON data (100k rows by default)
php bin/make_sample.php --rows=100000
# 3) Run the CSV demo
php examples/array_vs_generator.php # compare both
php examples/array_vs_generator.php array # only eager
php examples/array_vs_generator.php generator # only lazy
# 5) Run the API (NDJSON) streaming demo
php examples/batch_api_processing.php
Numbers from a Windows + OSPanel run (your mileage may vary):
Method | Time | Memory used | Peak diff | Rows |
---|---|---|---|---|
Array (eager) | 1.401s | 120 B | 395.92 MB | 1,000,001 |
Generator | 1.012s | 0 B | 0.00 MB | 1,000,001 |
These results show that the eager approach spikes peak memory (loading the whole dataset), while the generator keeps memory flat by streaming one row at a time.
- Increase the dataset size:
php bin/make_sample.php --rows=5000000
- Run array and generator separately to isolate their memory profiles:
php examples/array_vs_generator.php array php examples/array_vs_generator.php generator
- Optionally redirect output to a log:
php examples/array_vs_generator.php > results/benchmark.txt
Note: In the NDJSON benchmark, both eager and lazy modes show similar peak memory. This is because each decoded JSON object is still built fully into memory and processed in-place. Lazy reading helps mostly when:
- the source format is line-based or record-based (CSV, line-JSON),
- each record is processed incrementally and not accumulated.
In contrast, the CSV benchmark clearly benefits from lazy iteration, especially with millions of rows.
php ./vendor/bin/phpunit
MIT