This directory contains comprehensive performance benchmarks for the NINO validator package.
npm run benchmarkComprehensive benchmarking that includes:
- ✅ Function Performance: All core functions (validate, format, parse, generate)
- 📊 Input Pattern Analysis: Performance comparison across different input types
- 💾 Memory Usage: Memory consumption and leak detection
- 🔄 Bulk Operations: Realistic usage scenarios with large datasets
- 📈 Statistical Analysis: Detailed performance metrics and ratings
Features:
- High-resolution timing using
process.hrtime.bigint() - Memory usage tracking and delta calculation
- Warm-up iterations to ensure accurate measurements
- Performance ratings (Excellent/Good/Poor)
- Comprehensive reporting with insights
npm run benchmark:ciLightweight benchmarks suitable for CI/CD pipelines:
- ⚡ Fast Execution: Reduced iteration counts for quick feedback
- 🎯 Performance Gates: Automatic pass/fail based on thresholds
- 📋 Essential Metrics: Core performance indicators only
- 🚀 CI-Friendly: Exits with error codes for automated testing
Thresholds:
- Minimum acceptable: 1,000 operations/second
- Warning threshold: 10,000 operations/second
- Excellent threshold: 100,000 operations/second
npm run benchmark:memoryEnhanced benchmarks with garbage collection control:
- 🧠 Memory Profiling: Detailed memory usage analysis
- 🗑️ GC Control: Manual garbage collection for accurate measurements
- 📊 Memory Leak Detection: Identifies potential memory issues
- Operations/second: How many function calls can be executed per second
- Time per operation: Average time taken for a single function call (in microseconds)
- Memory delta: Memory usage change during benchmark execution
| Rating | Operations/Second | Description |
|---|---|---|
| 🟢 Excellent | ≥100,000 | Outstanding performance |
| 🟡 Good | ≥10,000 | Acceptable performance |
| 🔴 Poor | <10,000 | May need optimization |
Based on modern Node.js environments:
| Function | Expected Ops/Sec | Notes |
|---|---|---|
validateNINO (valid) |
100,000+ | Fast path validation |
validateNINO (invalid) |
80,000+ | May be slower due to error paths |
formatNINO |
50,000+ | String manipulation overhead |
parseNINO |
50,000+ | Object creation overhead |
generateRandomNINO |
1,000+ | Crypto operations are slower |
# Run comprehensive benchmarks
npm run benchmark
# Quick CI-style check
npm run benchmark:ci
# Memory-focused analysis
npm run benchmark:memoryThe benchmark:ci script is automatically included in the validate npm script and will run during:
- Pre-publish checks
- CI/CD pipeline validation
- Local validation with
npm run validate
The benchmark suite provides automatic insights such as:
- Comparison between valid vs invalid input performance
- Memory efficiency analysis
- Performance consistency across different input patterns
Consider performance investigation if:
- Any function performs below 1,000 ops/sec
- Memory usage grows unexpectedly
- Performance degrades significantly between versions
- CI benchmarks fail consistently
- Valid inputs typically perform faster than invalid ones
- Short-circuit validation can improve invalid input performance
- String operations may be optimized with different approaches
- Random generation performance depends on crypto operations
Uses process.hrtime.bigint() for nanosecond precision timing, ensuring accurate measurements even for very fast operations.
Monitors process.memoryUsage() before and after operations to detect memory leaks and usage patterns.
- Includes warm-up iterations to eliminate JIT compilation effects
- Uses large iteration counts for statistical significance
- Provides multiple metrics (ops/sec, time/op, memory usage)
- Tests various input patterns (valid, invalid, edge cases)
- Includes bulk operations that mirror real-world usage
- Tests string processing scenarios with messy inputs
When adding new benchmarks:
- Follow the existing pattern for function naming (
benchmark[FunctionName]) - Include appropriate iteration counts for the test type
- Add both individual and summary reporting
- Consider memory implications of your tests
- Update this README with new benchmark descriptions
Benchmarks running slowly:
- Reduce iteration counts in
ITERATIONSconfiguration - Check for background processes consuming CPU
- Ensure adequate system memory
Inconsistent results:
- Run multiple times and average results
- Check for system load during testing
- Consider using
--expose-gcflag for memory tests
CI failures:
- Review performance thresholds in
ci-benchmark.js - Check for environment-specific performance characteristics
- Consider adjusting thresholds for slower CI environments