Skip to main content

Pre-Allocating for slice, map

Allocating a slice with a capacity that is suitable for the number of elements it is expected to hold can improve performance in Go.

Here is the benchmark:

package main

import (
"fmt"
"time"
)

func main() {
// Allocate a slice with a small capacity
start := time.Now()
s := make([]int, 0, 10)
for i := 0; i < 100000; i++ {
s = append(s, i)
}
elapsed := time.Since(start)
fmt.Printf("Slice with small capacity: %v\n", elapsed) // 1.165208ms

// Allocate a slice with a larger capacity
start = time.Now()
s = make([]int, 0, 100000)
for i := 0; i < 100000; i++ {
s = append(s, i)
}
elapsed = time.Since(start)
fmt.Printf("Slice with larger capacity: %v\n", elapsed) // 361.333µs
}

This is because allocating a slice with a larger capacity can reduce the number of times that the slice needs to be resized as elements are added. See

Reference: https://medium.com/@func25/go-performance-boosters-the-top-5-tips-and-tricks-you-need-to-know-e5cf6e5bc683