GR

Groovy Performance

Performance optimization patterns for Groovy applications

Details

Language / Topic
groovyGroovy
Category
Performance

Rules

balanced
- Use `@CompileStatic` on hot-path classes to bypass Groovy's dynamic dispatch overhead and generate bytecode comparable to compiled Java.
- Prefer `List.collectMany {}` over nested `collect {}` with flatten to avoid creating intermediate lists in large data transformations.
- Use `LazyMap` and `@Lazy` initialization for expensive computed properties that may not always be accessed.
- Avoid overusing dynamic features (`methodMissing`, `propertyMissing`) in performance-critical paths — they add method lookup overhead on every call.
- Use `eachParallel {}` or Java `parallelStream()` for CPU-bound collection operations that can be safely parallelized.
- Cache results of expensive `@Memoized` method calls by annotating them with `@groovy.transform.Memoized` for pure functions.
- Prefer `StringBuilder` over GString concatenation in tight loops — GString creates intermediate strings on each interpolation.