Performance Benchmarks
Detailed performance benchmarks for Data Helpers.
Introduction
Section titled “Introduction”Data Helpers provides powerful features with acceptable performance overhead:
- Type safety and validation - With reasonable performance cost
- 3.6x faster than Other Serializer for complex mappings
- Other mapper libraries are up to 4.8x faster, but DataMapper provides better features
- Low memory footprint - ~1.2 KB per instance
Performance Trade-offs
Section titled “Performance Trade-offs”Data Helpers prioritizes developer experience, type safety and maintainability over raw speed:
SimpleDto #[UltraFast] vs Plain PHP:- SimpleDto: ~9.9μs per operation- Plain PHP: ~0.37μs per operation- Trade-off: ~27x slower, but with type safety, immutability and mapping
SimpleDto vs Plain PHP (without #[AutoCast]):- SimpleDto: ~17.1μs per operation- Plain PHP: ~0.37μs per operation- Trade-off: ~46x slower, but with type safety, validation and immutability
SimpleDto vs Plain PHP (with #[AutoCast]):- SimpleDto: ~17μs per operation (depending on casting needs)- Plain PHP: ~0.4μs per operation- Trade-off: ~47x slower, but with automatic type conversion- Note: Only use #[AutoCast] when you need automatic type conversion (e.g., CSV, XML, HTTP requests with string values)
DataMapper vs Plain PHP:- DataMapper: ~13-15μs per operation- Plain PHP: ~0.1-0.3μs per operation- Trade-off: ~69x slower, but with template syntax and automatic mapping
DataMapper vs Other Serializer:- DataMapper: ~17-21μs per operation- OtherSerializer: ~62-76μs per operation- Benefit: 3.6x faster with better developer experienceAutoCast Performance Impact
Section titled “AutoCast Performance Impact”The #[AutoCast] attribute provides automatic type conversion but comes with a performance cost:
Scenario 1: Correct types (no casting needed)- SimpleDto (no AutoCast): ~17μs (46x slower than Plain PHP)- SimpleDto (with AutoCast): ~17μs (47x slower than Plain PHP)- AutoCast overhead: ~0%
Scenario 2: String types (casting needed)- SimpleDto (with AutoCast): ~17μs (47x slower than Plain PHP)- Casting overhead: ~1% (compared to correct types)Key Insights:
- #[AutoCast] adds ~0% overhead even when no casting is needed (due to reflection)
- Actual casting adds only ~1% overhead on top of the AutoCast overhead
- Without #[AutoCast], SimpleDto is ~1.0x faster and closer to Plain PHP performance
When to use #[AutoCast]:
- ✅ CSV imports (all values are strings)
- ✅ XML parsing (all values are strings)
- ✅ HTTP requests (query params and form data are strings)
- ✅ Legacy APIs with inconsistent types
- ❌ Internal Dtos with correct types
- ❌ Performance-critical code paths
- ❌ High-throughput data processing
When to Use Data Helpers
Section titled “When to Use Data Helpers”Use Data Helpers when:
- You need type safety and validation
- You work with complex data structures
- You want maintainable, readable code
- Performance is acceptable (not in tight loops)
- You’re replacing Symfony Serializer or other heavy libraries
Consider Plain PHP or LiteDto when:
- You’re in performance-critical tight loops
- You process millions of operations per second
- You don’t need validation or type safety
- You’re willing to write and maintain manual mapping code
DataAccessor Performance
Section titled “DataAccessor Performance”| Operation | Time | Description |
|---|---|---|
| Simple Get | 0.061μs | Get value from flat array |
| Nested Get | 0.490μs | Get value from nested path |
| Wildcard Get | 0.917μs | Get values using single wildcard |
| Deep Wildcard Get | 38.993μs | Get values using multiple wildcards |
| Typed Get String | 0.086μs | Get typed string value |
| Typed Get Int | 0.083μs | Get typed int value |
| Create Accessor | 0.060μs | Instantiate DataAccessor |
DataMutator Performance
Section titled “DataMutator Performance”| Operation | Time | Description |
|---|---|---|
| Simple Set | 1.162μs | Set value in flat array |
| Nested Set | 1.444μs | Set value in nested path |
| Deep Set | 1.579μs | Set value creating new nested structure |
| Multiple Set | 2.048μs | Set multiple values at once |
| Merge | 1.467μs | Deep merge arrays |
| Unset | 1.395μs | Remove single value |
| Multiple Unset | 1.850μs | Remove multiple values |
| Wildcard Set | 1.976μs | |
| Deep Wildcard Set | 5.321μs |
DataMapper Performance
Section titled “DataMapper Performance”| Operation | Time | Description |
|---|---|---|
| Simple Mapping | 12.234μs | Map flat structure |
| Nested Mapping | 12.735μs | Map nested structure |
| Auto Map | 10.091μs | Automatic field mapping |
| Map From Template | 13.389μs | Map using template expressions |
Memory Usage
Section titled “Memory Usage”Dto Instance: ~1.2 KBWith Validation: ~1.5 KBWith Caching: ~0.8 KBDto Performance Comparison
Section titled “Dto Performance Comparison”Comparison of our SimpleDto implementation with other Dto libraries and plain PHP:
| Implementation | From Array | To Array | Complex Data |
|---|---|---|---|
| SimpleDto Normal | 6.299μs | 38.534μs | 6.321μs |
| SimpleDto #[UltraFast] | 5.708μs (1.1x faster) | 37.787μs | 5.742μs (1.1x faster) |
| LiteDto | 3.247μs (1.9x faster) | 6.447μs (6.0x faster) | 3.226μs (2.0x faster) |
| LiteDto #[UltraFast] | 2.642μs (2.4x faster) | 4.728μs (8.1x faster) | 2.598μs (2.4x faster) |
| Plain PHP | 0.111μs (56.5x faster) | - | - |
| Other Dtos | 3.305μs (1.9x faster) | 4.002μs (9.6x faster) | 3.328μs (1.9x faster) |
Key Insights:
- #[UltraFast] mode provides 1.7x faster performance than normal SimpleDto
- #[UltraFast] is only ~96x slower than Plain PHP (vs ~166x for normal mode)
- #[UltraFast] is competitive with other Dto libraries (~3x slower)
- SimpleDto provides type safety, validation and immutability with reasonable overhead
- The overhead is acceptable for the added safety and developer experience
Mapper Performance Comparison
Section titled “Mapper Performance Comparison”Comparison of our DataMapper with other mapper libraries and plain PHP:
| Implementation | Simple Mapping | Nested Mapping | Template Mapping |
|---|---|---|---|
| DataMapper | 12.279μs | 14.700μs | 15.123μs |
| SimpleDto #[UltraFast] | 5.708μs (2.2x faster) | 10.751μs (1.4x faster) | - |
| Plain PHP | 0.067μs (184.1x faster) | 0.136μs (107.7x faster) | - |
| Other Mappers | 2.566μs (4.8x faster) | N/A | N/A |
Key Insights:
- SimpleDto #[UltraFast] is 2.2x faster than DataMapper for simple mapping
- Other mapper libraries are up to 4.8x faster than DataMapper, and 2.2x faster than #[UltraFast]
- Plain PHP is ~184x faster but requires manual mapping code for each use case
- DataMapper provides the best balance of features, readability and maintainability for complex mappings
Serialization Performance
Section titled “Serialization Performance”Comparison with external serializers for nested JSON to Dto mapping:
| Implementation | Template Syntax | Simple Paths |
|---|---|---|
| DataMapper | 22.625μs | 15.933μs |
| SimpleDto #[UltraFast] | 5.708μs (4.0x faster) | 5.708μs (2.8x faster) |
| Plain PHP | 0.322μs (70.3x faster) | 0.322μs (49.5x faster) |
| Other Serializer | 69.100μs (3.1x slower) | 69.100μs (4.3x slower) |
Key Insights:
- SimpleDto #[UltraFast] is 12.1x faster than Other Serializer!
- SimpleDto #[UltraFast] is 3.4x faster than DataMapper for simple mappings
- DataMapper is 3.6x faster than Other Serializer for complex mappings
- Zero reflection overhead for template-based mapping
- Optimized for nested data structures
Cache Invalidation Performance
Section titled “Cache Invalidation Performance”Data Helpers supports different cache invalidation strategies with varying performance characteristics:
Cache Invalidation Modes (50,000 iterations, warm cache):- MANUAL (no validation): 2.62 μs- MTIME (auto-validation): 2.68 μs- HASH (auto-validation): 2.84 μsPerformance Attributes
Section titled “Performance Attributes”Skip unnecessary operations for maximum DTO instantiation speed:
Basic Dto (10,000 iterations)
Section titled “Basic Dto (10,000 iterations)”Normal Dto: 1.72 μs (baseline)#[UltraFast]: 1.56 μs (9.1% faster)#[NoCasts]: 1.05 μs (39.0% faster)#[NoValidation]: 1.83 μs (6.5% slower)#[NoAttributes]: 1.75 μs (1.6% slower)#[NoCasts, NoValidation]: 1.10 μs (36.2% faster)#[NoAttributes, NoCasts]: 1.72 μs (0.2% faster)With AutoCast (10,000 iterations)
Section titled “With AutoCast (10,000 iterations)”AutoCast Dto: 1.79 μs (with type casting)#[NoCasts]: 1.02 μs (43.0% faster)Real-World API (1,000 Dtos)
Section titled “Real-World API (1,000 Dtos)”SimpleDto: 1.72 ms#[UltraFast]: 1.56 ms (9.1% faster)#[NoCasts]: 1.05 ms (39.0% faster)#[NoAttributes, NoCasts]: 1.72 ms (0.2% faster)
Savings per 1M requests: ~157ms (0.2s) with #[UltraFast]See Also
Section titled “See Also”- Running Benchmarks - How to run
- Optimization - Optimization guide
- SimpleDto Caching - Cache invalidation strategies
- Cache Generation Guide - Manual cache generation