Skip to content

Performance Optimization

Complete guide for optimizing Data Helpers performance.

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
Terminal window
php artisan dto:cache
Terminal window
bin/console dto:cache
use event4u\DataHelpers\SimpleDTO\Cache\ValidationCache;
ValidationCache::enable();
ValidationCache::warmup();
Without Cache: 5,000 validations/sec
With Cache: 990,000 validations/sec
Improvement: 198x faster
use event4u\DataHelpers\SimpleDTO\Attributes\Lazy;
class UserDTO extends SimpleDTO
{
public function __construct(
public readonly string $name,
#[Lazy]
public readonly ?array $posts = null,
) {}
}
config/data-helpers.php
return [
'casts' => [
'cache_instances' => true,
],
];
// ✅ Good - 2-3 levels
$dto->address->city;
// ❌ Bad - too deep
$dto->company->department->team->manager->address->city;
// ✅ Good
public readonly int $age;
// ❌ Bad
public readonly mixed $age;
// ✅ Good
$dtos = DataCollection::make($users, UserDTO::class);
// ❌ Bad
foreach ($users as $user) {
$dtos[] = UserDTO::fromModel($user);
}
// Use chunking
User::chunk(1000, function($users) {
$dtos = DataCollection::make($users, UserDTO::class);
});
  • 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