dart-optimization
Installation
SKILL.md
Optimization & Debugging
Performance in Dart goes beyond UI rendering; it's about efficient execution, smart resource utilization, and sound error handling.
Dart Performance Patterns
- Standardize Types: Avoid
dynamic. Use explicit types orObject?. Statically typed code allows the compiler to perform far better optimizations. - Efficient Collections:
- Use
Setfor average O(1) containment checks. - Use
Listfor ordered indexing. - Prefer
Iterablemethods (map,where) for readability, but useforloops in performance-critical hot paths.
- Use
- Inlining: Small getters and trivial functions are often inlined by the VM/AOT, but keeping them simple ensures this optimization happens.
Compile-Time Optimizations
- Final & Const: Declare variables as
finalwhenever possible. Useconstconstructors for widgets and data models to enable compile-time allocation and reduce runtime garbage collection pressure. - Ternary vs If-Else: In Dart, they are generally equivalent, but prioritize readability. Use
switchexpressions (Dart 3+) for exhaustive and efficient pattern matching. - Extension Types: Use Dart 3.3+
extension typezero-cost abstractions for type wrapping to eliminate runtime allocation overhead (see dart-modern-syntax). - Private Named Parameters: Use Dart 3.12+
MyClass({this._privateField})constructors to eliminate initializer list code boilerplate (see dart-modern-syntax).