Optimizing Chart Performance

1. Minimizing Template Complexity

Anti-patternFix
Deeply nested conditionalsExtract to named template
Repeated includeCache result in variable
Complex inline expressionsCompute once into variable

2. Caching Template Results

Example: Cache expensive include

{{- $labels := include "myapp.labels" . -}}
metadata:
  labels: {{- $labels | nindent 4 }}
---
spec:
  template:
    metadata:
      labels: {{- $labels | nindent 8 }}

3. Reducing Value Lookups

Example: Bind once with with

{{- with .Values.resources }}
resources:
  requests: { cpu: {{ .cpuRequest }}, memory: {{ .memRequest }} }
  limits:   { cpu: {{ .cpuLimit }},   memory: {{ .memLimit }} }
{{- end }}

4. Optimizing Loop Iterations

PracticeWhy
Filter before rangeReduce iteration count
Hoist invariantsCompute outside range
Avoid nested rangesO(n²) renders are slow

5. Using Conditional Rendering

Example: Skip unused blocks

{{- if .Values.metrics.enabled }}
{{- include "myapp.metrics-service" . }}
{{- include "myapp.servicemonitor" . }}
{{- end }}

6. Minimizing File Reads

PracticeDetail
Cache .Files.GetRead once into variable, reuse
Use GlobIterate matched files instead of listing manually
Avoid huge filesMove large blobs to ConfigMap mounted at runtime

7. Reducing Chart Size

ActionEffect
Add .helmignoreExclude tests, docs, CI files from package
Remove unused subchartsUse condition:
Minify embedded filesStrip whitespace from JSON/YAML blobs

8. Optimizing Dependencies

9. Using Lazy Evaluation

Example: Short-circuit expensive checks

{{- if and .Values.lookup.enabled (eq .Release.IsInstall false) }}
{{- $existing := lookup "v1" "Secret" .Release.Namespace "db-creds" -}}
{{- end }}

10. Profiling Chart Rendering

Example: Measure render time

time helm template ./chart -f values-prod.yaml > /dev/null
# Compare branches
hyperfine 'helm template ./chart-old' 'helm template ./chart-new'