← All benchmarksGo

Go slice preallocation: 0.95 ms vs 4.49 ms, 1 vs 39 allocations

Does preallocating a slice with make improve Go performance?

Yes. Appending 1,000,000 ints took 4.49 ms, 41.7 MB and 39 allocations without preallocation, and 0.95 ms, 8.0 MB and 1 allocation with make([]int, 0, N).

Results

append to nil slice4.49 ms/op, 41.7 MB/op, 39 allocs/op
make([]int, 0, N)0.95 ms/op, 8.0 MB/op, 1 alloc/op

How it was measured

go test -bench, 20 iterations, N = 1,000,000.

s := make([]int, 0, N)
for j := 0; j < N; j++ { s = append(s, j) }

Why

A nil slice regrows (and copies) several times. A preallocated capacity allocates once.

When this does not apply

Only helps when you know the size up front.

Environment

MacBook Air (Apple silicon, 8 cores), macOS, Go 1.22. Measured 2026-10-12.

Source: shorts/demo-ticket/go/prealloc/prealloc_test.go in my content-engine repository. Treat any single run as a direction; re-run it on your hardware.

Related

By Josué Barros, senior software engineer (full-stack, data and AI).