String Concatination — Go Benchmark
Comparing string concatenation approaches in Go — from the naive + operator to strings.Builder and beyond.
Compares four common ways to build a string incrementally in Go. Each implementation appends the same character repeatedly, letting the final string grow over time to expose performance differences. The write behavior measures the cost of appending, while read measures the cost of materializing the final string.
1 CPU
Fastest
Simple AppendSlowest
Append To Slice And JoinFastest
String BuilderSlowest
Simple Append32 CPUs
Fastest
Simple AppendSlowest
Append To Slice And JoinFastest
String BuilderSlowest
Simple AppendSimple Append
Fastest (Read)Slowest (Write)Uses the + operator to concatenate strings. Each append copies the entire string, making the total cost O(n²). Reading is free since the result is already a string.
String Builder
Fastest (Write)Uses strings.Builder to accumulate strings. Write is amortized O(1). Reading calls builder.String(), which uses an unsafe conversion and does not allocate.
Buffer
Uses bytes.Buffer to accumulate strings. Write is amortized O(1). Reading calls buf.String(), which copies the internal buffer into a new string (allocates).
Append To Slice And Join
Slowest (Read)Appends strings to a slice with append, then calls strings.Join to produce the final result. Write is cheap (slice append), but read iterates and allocates the joined string.
Contributors