Quick Wins
Enable Caching
Cache expressions and paths (enabled by default)
Reuse Engine
Create once, use many times
Order Rules
Put fastest checks first
Batch Evaluate
Evaluate multiple rules at once
Caching
Expression Cache
Caches evaluated expressions for repeated use.- ✅ 10-20x faster for repeated rules
- ✅ Automatic LRU eviction
- ✅ Memory efficient
- Highly dynamic rules
- Memory constraints
- Rules evaluated once
Path Cache
Caches path resolutions.Rule Ordering
Put fastest checks first to fail fast.Performance Hierarchy
- Fastest: Simple comparisons (
eq,neq,gt,lt) - Fast: Array checks (
in,notIn) - Medium: String operations (
contains,startsWith) - Slow: Regex patterns (
regex) - Slowest: State changes (
changed,increased)
Bad vs Good
- ❌ Bad - Slow First
- ✅ Good - Fast First
Reuse Engine
Create once, reuse everywhere.- ❌ Bad
- ✅ Good
Batch Operations
UseStatefulRuleEngine.evaluateBatch() for multiple rules.
- Single context preparation
- Shared path resolutions
- Better cache utilization
Minimize Depth
Flatten nested rules when possible.- Nested (Slower)
- Flat (Faster)
Monitoring
Get Metrics
Cache Stats
Benchmarking
Real-World Optimization
- Before
- After
Best Practices
1. Enable Caching (Default)
1. Enable Caching (Default)
2. Reuse Engine Instance
2. Reuse Engine Instance
3. Order Conditions
3. Order Conditions
4. Use Batch Operations
4. Use Batch Operations
5. Monitor Metrics
5. Monitor Metrics
Performance Checklist
- Cache enabled
- Reusing engine instance
- Fast checks first in
andrules - Slow checks last
- Using batch operations
- Monitoring metrics
- Rules flattened (not deeply nested)
- Clear cache when data schema changes
