Performance Optimization
Complete guide for optimizing Data Helpers performance.
Introduction
Section titled “Introduction”Data Helpers provides multiple optimization techniques:
- ✅ Validation Caching - 198x faster validation
- ✅ Lazy Loading - Defer expensive operations
- ✅ Type Cast Caching - Reuse cast instances
- ✅ Path Compilation - Pre-compile dot-notation paths
Enable Validation Caching
Section titled “Enable Validation Caching”Laravel
Section titled “Laravel”php artisan dto:cacheSymfony
Section titled “Symfony”bin/console dto:cachePlain PHP
Section titled “Plain PHP”use event4u\DataHelpers\SimpleDTO\Cache\ValidationCache;
ValidationCache::enable();ValidationCache::warmup();Performance Impact
Section titled “Performance Impact”Without Cache: 5,000 validations/secWith Cache: 990,000 validations/sec
Improvement: 198x fasterUse Lazy Loading
Section titled “Use Lazy Loading”use event4u\DataHelpers\SimpleDTO\Attributes\Lazy;
class UserDTO extends SimpleDTO{ public function __construct( public readonly string $name,
#[Lazy] public readonly ?array $posts = null, ) {}}Optimize Type Casting
Section titled “Optimize Type Casting”return [ 'casts' => [ 'cache_instances' => true, ],];Avoid Deep Nesting
Section titled “Avoid Deep Nesting”// ✅ Good - 2-3 levels$dto->address->city;
// ❌ Bad - too deep$dto->company->department->team->manager->address->city;Use Specific Types
Section titled “Use Specific Types”// ✅ Goodpublic readonly int $age;
// ❌ Badpublic readonly mixed $age;Batch Operations
Section titled “Batch Operations”// ✅ Good$dtos = DataCollection::make($users, UserDTO::class);
// ❌ Badforeach ($users as $user) { $dtos[] = UserDTO::fromModel($user);}Memory Optimization
Section titled “Memory Optimization”// Use chunkingUser::chunk(1000, function($users) { $dtos = DataCollection::make($users, UserDTO::class);});Best Practices
Section titled “Best Practices”- Enable validation caching in production
- Use lazy loading for expensive operations
- Enable cast caching
- Avoid deep nesting (max 3 levels)
- Use specific types instead of mixed
- Use batch operations
- Use chunking for large datasets
See Also
Section titled “See Also”- Performance Benchmarks - Detailed benchmarks
- Lazy Properties - Lazy loading guide