Slices and strings in Go look simple β until they eat your RAM and GC pauses.
β
Pre-allocate with make([]T, 0, N) to skip hidden copies
β
Use strings.Builder for clean, fast string joins
β
sync.Pool = free GC breaks during traffic spikes
β
Reuse buffers, reset with care, avoid race bugs
π₯ Fresh tricks:
- Filter slices in place: filtered := events[:0] β no new allocs
- One static HTML builder instead of 20 tiny buffers
- Reuse JSON encoders to slash latency under load
Takeaway:
Know how slices grow, pick the right string concat method, and treat pools like sharp knives β powerful but dangerous.
π Less GC, more QPS. Go beyond βit worksβ to βit flies.β
https://levelup.gitconnected.com/optimizing-go-code-slices-strings-and-sync-pool-in-2025-048fc6d79ecb
Top comments (0)