seniorGolang
Optimizing slice allocations using capacity.
Updated Apr 28, 2026
Short answer
Pre-allocating slice capacity prevents expensive memory reallocations and copies during append operations.
Deep explanation
When a slice reaches its capacity, Go allocates a new array (usually 2x the size for small slices) and copies elements. If you loop 10,000 times appending to a slice, this reallocation happens roughly 14 times, causing massive CPU and GC overhead. make([]T, length, capacity) eliminates this.
Unlock with a Pro subscription to view this section.
View pricingReal-world example
No real-world example available yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProCommon mistakes
No common mistakes listed yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProFollow-up questions
No follow-up questions available yet.
Unlock with a Pro subscription to view this section.
Upgrade to Pro