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.5x faster than Other Serializer for complex mappings
- Other mapper libraries are up to 4.9x 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: ~8.1μs per operation- Plain PHP: ~0.34μs per operation- Trade-off: ~24x slower, but with type safety, immutability and mapping
SimpleDto vs Plain PHP (without #[AutoCast]):- SimpleDto: ~15.0μs per operation- Plain PHP: ~0.34μs per operation- Trade-off: ~44x slower, but with type safety, validation and immutability
SimpleDto vs Plain PHP (with #[AutoCast]):- SimpleDto: ~15-16μs per operation (depending on casting needs)- Plain PHP: ~0.3μs per operation- Trade-off: ~44-46x 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: ~10-13μs per operation- Plain PHP: ~0.1-0.2μs per operation- Trade-off: ~77x slower, but with template syntax and automatic mapping
DataMapper vs Other Serializer:- DataMapper: ~15-18μs per operation- OtherSerializer: ~52-64μs per operation- Benefit: 3.5x 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): ~15μs (44x slower than Plain PHP)- SimpleDto (with AutoCast): ~15μs (44x slower than Plain PHP)- AutoCast overhead: ~0%
Scenario 2: String types (casting needed)- SimpleDto (with AutoCast): ~16μs (46x slower than Plain PHP)- Casting overhead: ~3% (compared to correct types)Key Insights:
- #[AutoCast] adds ~0% overhead even when no casting is needed (due to reflection)
- Actual casting adds only ~3% 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.044μs | Get value from flat array |
| Nested Get | 0.367μs | Get value from nested path |
| Wildcard Get | 0.680μs | Get values using single wildcard |
| Deep Wildcard Get | 30.771μs | Get values using multiple wildcards |
| Typed Get String | 0.065μs | Get typed string value |
| Typed Get Int | 0.063μs | Get typed int value |
| Create Accessor | 0.046μs | Instantiate DataAccessor |
DataMutator Performance
Section titled “DataMutator Performance”| Operation | Time | Description |
|---|---|---|
| Simple Set | 0.876μs | Set value in flat array |
| Nested Set | 1.100μs | Set value in nested path |
| Deep Set | 1.201μs | Set value creating new nested structure |
| Multiple Set | 1.518μs | Set multiple values at once |
| Merge | 1.081μs | Deep merge arrays |
| Unset | 1.074μs | Remove single value |
| Multiple Unset | 1.407μs | Remove multiple values |
| Wildcard Set | 1.525μs | |
| Deep Wildcard Set | 3.987μs |
DataMapper Performance
Section titled “DataMapper Performance”| Operation | Time | Description |
|---|---|---|
| Simple Mapping | 10.024μs | Map flat structure |
| Nested Mapping | 10.376μs | Map nested structure |
| Auto Map | 8.073μs | Automatic field mapping |
| Map From Template | 11.113μ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 | 5.653μs | 31.815μs | 5.662μs |
| SimpleDto #[UltraFast] | 4.674μs (1.2x faster) | 30.065μs | 4.643μs (1.2x faster) |
| LiteDto | 3.669μs (1.5x faster) | 6.542μs (4.9x faster) | 3.683μs (1.5x faster) |
| LiteDto #[UltraFast] | 2.323μs (2.4x faster) | 4.354μs (7.3x faster) | 2.343μs (2.4x faster) |
| Plain PHP | 0.079μs (71.3x faster) | - | - |
| Other Dtos | 2.674μs (2.1x faster) | 3.283μs (9.7x faster) | 2.655μs (2.1x faster) |
Key Insights:
- #[UltraFast] mode provides 1.8x faster performance than normal SimpleDto
- #[UltraFast] is only ~111x slower than Plain PHP (vs ~197x 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 | 10.118μs | 12.256μs | 12.356μs |
| SimpleDto #[UltraFast] | 4.674μs (2.2x faster) | 8.733μs (1.4x faster) | - |
| Plain PHP | 0.048μs (212.3x faster) | 0.103μs (118.6x faster) | - |
| Other Mappers | 2.060μs (4.9x 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.9x faster than DataMapper, and 2.3x faster than #[UltraFast]
- Plain PHP is ~212x 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 | 19.707μs | 13.384μs |
| SimpleDto #[UltraFast] | 4.674μs (4.2x faster) | 4.674μs (2.9x faster) |
| Plain PHP | 0.255μs (77.2x faster) | 0.255μs (52.4x faster) |
| Other Serializer | 58.289μs (3.0x slower) | 58.289μs (4.4x slower) |
Key Insights:
- SimpleDto #[UltraFast] is 12.5x faster than Other Serializer!
- SimpleDto #[UltraFast] is 3.5x faster than DataMapper for simple mappings
- DataMapper is 3.5x 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.71 μs- MTIME (auto-validation): 2.71 μs- HASH (auto-validation): 2.72 μ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.62 μs (baseline)#[UltraFast]: 1.24 μs (23.3% faster)#[NoCasts]: 1.10 μs (32.3% faster)#[NoValidation]: 1.57 μs (3.1% faster)#[NoAttributes]: 1.57 μs (3.1% faster)#[NoCasts, NoValidation]: 1.07 μs (34.0% faster)#[NoAttributes, NoCasts]: 1.58 μs (2.2% faster)With AutoCast (10,000 iterations)
Section titled “With AutoCast (10,000 iterations)”AutoCast Dto: 1.65 μs (with type casting)#[NoCasts]: 1.07 μs (35.6% faster)Real-World API (1,000 Dtos)
Section titled “Real-World API (1,000 Dtos)”SimpleDto: 1.62 ms#[UltraFast]: 1.24 ms (23.3% faster)#[NoCasts]: 1.10 ms (32.3% faster)#[NoAttributes, NoCasts]: 1.58 ms (2.2% faster)
Savings per 1M requests: ~376ms (0.4s) 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