Benchmarks
Performance comparison between govalid and reflection-based validators
Performance Benchmarks
govalid is designed for maximum performance with zero allocations. Here are the latest benchmark results comparing govalid with reflection-based validators.
Latest Results
Benchmarked on: 2025-07-15
Platform: Darwin 24.5.0 arm64
Go version: go1.25-devel_a8e99ab19c
Raw Benchmark Data
|
|
Performance Comparison
vs go-playground/validator
Validator | govalid (ns/op) | go-playground/validator (ns/op) | Improvement | govalid Allocs | Competitor Allocs |
---|---|---|---|---|---|
38.24ns | 649.4 | 17.0x faster | 0 allocs/op | 5 allocs + 90 B/op | |
GT | 1.936ns | 63.00 | 32.5x faster | 0 allocs/op | 0 allocs/op |
GTE | 1.946ns | 60.30 | 31.0x faster | 0 allocs/op | 0 allocs/op |
LT | 1.916ns | 60.77 | 31.7x faster | 0 allocs/op | 0 allocs/op |
LTE | 1.969ns | 61.91 | 31.4x faster | 0 allocs/op | 0 allocs/op |
MaxItems | 2.520ns | 80.30 | 31.9x faster | 0 allocs/op | 0 allocs/op |
MaxLength | 15.67ns | 73.50 | 4.7x faster | 0 allocs/op | 0 allocs/op |
MinItems | 2.785ns | 79.81 | 28.7x faster | 0 allocs/op | 0 allocs/op |
MinLength | 11.60ns | 67.51 | 5.8x faster | 0 allocs/op | 0 allocs/op |
Required | 1.935ns | 85.51 | 44.2x faster | 0 allocs/op | 0 allocs/op |
URL | 42.54ns | 287.9 | 6.8x faster | 0 allocs/op | 1 allocs + 144 B/op |
UUID | 37.69ns | 254.0 | 6.7x faster | 0 allocs/op | 0 allocs/op |
Enum | 2.242ns | N/A (govalid exclusive) | govalid exclusive | 0 allocs/op | N/A |
vs go-validator/validator
Validator | govalid (ns/op) | go-validator/validator (ns/op) | Improvement | govalid Allocs | Competitor Allocs |
---|---|---|---|---|---|
36.80ns | 584.1 | 15.9x faster | 0 allocs/op | 0 allocs/op | |
GT | 1.825ns | 53.57 | 29.4x faster | 0 allocs/op | 0 allocs/op |
MaxLength | 15.58ns | 155.7 | 10.0x faster | 0 allocs/op | 2 allocs + 32 B/op |
MinLength | 11.46ns | 159.6 | 13.9x faster | 0 allocs/op | 2 allocs + 32 B/op |
Required | 1.914ns | 1.929 | 1.0x faster | 0 allocs/op | 0 allocs/op |
URL | 41.68ns | 7776 | 186.5x faster | 0 allocs/op | 1 allocs + 146 B/op |
UUID | 36.21ns | 193.1 | 5.3x faster | 0 allocs/op | 0 allocs/op |
vs ozzo-validation
Validator | govalid (ns/op) | ozzo-validation (ns/op) | Improvement | govalid Allocs | Competitor Allocs |
---|---|---|---|---|---|
36.80ns | 39.35 | 1.1x faster | 0 allocs/op | 1 allocs + 24 B/op | |
MaxLength | 15.58ns | 159.3 | 10.2x faster | 0 allocs/op | 4 allocs + 432 B/op |
Required | 1.914ns | 33.69 | 17.6x faster | 0 allocs/op | 1 allocs + 24 B/op |
URL | 41.68ns | 7739 | 185.6x faster | 0 allocs/op | 2 allocs + 170 B/op |
UUID | 36.21ns | 231.5 | 6.4x faster | 0 allocs/op | 1 allocs + 24 B/op |
vs gookit/validate
Validator | govalid (ns/op) | gookit/validate (ns/op) | Improvement | govalid Allocs | Competitor Allocs |
---|---|---|---|---|---|
36.80ns | 9559 | 259.8x faster | 0 allocs/op | 74 allocs + 15952 B/op | |
MaxLength | 15.58ns | 9125 | 585.5x faster | 0 allocs/op | 80 allocs + 15632 B/op |
Required | 1.914ns | 8698 | 4543.1x faster | 0 allocs/op | 72 allocs + 15472 B/op |
URL | 41.68ns | 9216 | 221.1x faster | 0 allocs/op | 75 allocs + 15641 B/op |
UUID | 36.21ns | 9256 | 255.6x faster | 0 allocs/op | 74 allocs + 15514 B/op |
Key Performance Insights
1. Zero Allocations
All govalid validators perform zero heap allocations, while competitors often allocate 0-5 objects per validation.
2. Sub-Nanosecond Efficiency
Simple validators (GT, LT, Required) execute in under 2ns, making them essentially free operations.
3. Complex Validation Optimization
Even complex validators like email and URL are optimized with:
- Manual string parsing (no regex overhead)
- Single-pass validation algorithms
- Zero memory allocations
4. String Length Performance
Unicode-aware string validators are 4.8-6.0x faster despite proper UTF-8 handling.
govalid-Exclusive Features
Enum Validation
|
|
- Performance: ~2.2ns with 0 allocations
- No equivalent in go-playground/validator
- Supports string, numeric, and custom types
Extended Collection Support
|
|
Optimization Techniques
1. Code Generation
- Compile-time validation functions (no runtime reflection)
- Inlined simple operations for maximum speed
- Direct field access with no interface overhead
2. External Helper Functions
Complex validators use optimized external functions for better performance.
3. Manual String Parsing
- Character-by-character parsing instead of
strings.Split
- Direct indexing instead of
strings.Contains
- Single-pass algorithms for complex validation
4. Memory Optimization
- Zero heap allocations across all validators
- Stack-only operations for maximum cache efficiency
- Minimal memory footprint in generated code
Running Benchmarks
To run benchmarks yourself:
|
|
Conclusion
govalid delivers exceptional performance improvements across all reflection-based validators:
Performance Improvements by Library
- vs go-playground/validator: 4.8x to 44.8x faster
- vs go-validator/validator: 1.0x to 186.5x faster
- vs ozzo-validation: 1.1x to 185.6x faster
- vs gookit/validate: 221.1x to 4543.1x faster
Additional Benefits
- Zero allocations across all validators (vs 0-80 allocs for competitors)
- Sub-3ns performance for simple operations
- Extended type support (maps, channels, enums)
- Production-ready with comprehensive test coverage
Choose govalid when performance matters and zero allocations are critical for your application’s success.