Go string concatenation benchmark

TL;DR strings.Builder is the fastest and takes least memory. + is tied in memory but slightly less performant (although much more convenient). strings.Join is not a bad alternative for arrays. fmt.Sprintf is trash. Check the repo in which I share the benchmarks so you can test them too! Have you ever wondered what the fastest way to concatenate strings is? Well I did today, so I whipped up a quick benchmark (since a Google search was not yielding me any conclusive results) and as of today, 1st of May 2025, these are the results: goos: linux goarch: amd64 pkg: scratchpad-go cpu: AMD Ryzen 5 5600X 6-Core Processor BenchmarkConcatStrings_Plus-12 51331621 21.97 ns/op 8 B/op 1 allocs/op BenchmarkConcatStrings_Sprintf-12 11596424 102.9 ns/op 40 B/op 3 allocs/op BenchmarkConcatStrings_StringsBuilder-12 64022565 18.46 ns/op 8 B/op 1 allocs/op BenchmarkConcatStrings_Join-12 42522718 27.70 ns/op 8 B/op 1 allocs/op BenchmarkConcatStringAndInt_Plus-12 51975021 22.21 ns/op 5 B/op 1 allocs/op BenchmarkConcatStringAndInt_Sprintf-12 12212780 98.96 ns/op 21 B/op 2 allocs/op BenchmarkConcatStringAndInt_StringsBuilder-12 56754538 20.43 ns/op 8 B/op 1 allocs/op BenchmarkConcatStringAndInt_Join-12 38538142 28.96 ns/op 8 B/op 1 allocs/op

May 1, 2025 - 20:24
 0
Go string concatenation benchmark

TL;DR

strings.Builder is the fastest and takes least memory.
+ is tied in memory but slightly less performant (although much more convenient).
strings.Join is not a bad alternative for arrays.
fmt.Sprintf is trash.

Check the repo in which I share the benchmarks so you can test them too!

Have you ever wondered what the fastest way to concatenate strings is?

Well I did today, so I whipped up a quick benchmark (since a Google search was not yielding me any conclusive results) and as of today, 1st of May 2025, these are the results:

goos: linux
goarch: amd64
pkg: scratchpad-go
cpu: AMD Ryzen 5 5600X 6-Core Processor             
BenchmarkConcatStrings_Plus-12                      51331621            21.97 ns/op        8 B/op          1 allocs/op
BenchmarkConcatStrings_Sprintf-12                   11596424           102.9 ns/op        40 B/op          3 allocs/op
BenchmarkConcatStrings_StringsBuilder-12            64022565            18.46 ns/op        8 B/op          1 allocs/op
BenchmarkConcatStrings_Join-12                      42522718            27.70 ns/op        8 B/op          1 allocs/op
BenchmarkConcatStringAndInt_Plus-12                 51975021            22.21 ns/op        5 B/op          1 allocs/op
BenchmarkConcatStringAndInt_Sprintf-12              12212780            98.96 ns/op       21 B/op          2 allocs/op
BenchmarkConcatStringAndInt_StringsBuilder-12       56754538            20.43 ns/op        8 B/op          1 allocs/op
BenchmarkConcatStringAndInt_Join-12                 38538142            28.96 ns/op        8 B/op          1 allocs/op