Do Go Generics Really Outperform interface{}? A Detailed Benchmark
This article presents a systematic benchmark comparing Go's new generic types with the traditional interface{} approach, measuring execution time and memory allocation on identical hardware to determine whether generics provide a measurable performance advantage.
Introduction
With the release of Go 1.18, generics were added to the language, promising more flexible and type‑safe code as well as potential performance gains. However, the community remains divided on whether generics actually improve runtime speed compared to the classic interface{} usage.
Generics vs. interface{}
In Go, interface{} is a catch‑all type that can hold any value but often requires runtime type assertions, incurring overhead. Generics, introduced in Go 1.18, resolve types at compile time, eliminating many of those checks and reducing code duplication.
Benchmark Design
Test Goals
Compare the performance of two implementations that perform identical work:
Using interface{} for data handling.
Using generics for the same data handling.
Test Environment
Go version: 1.22
OS: Windows 11
CPU: Intel Core Ultra 7 155H
Memory: 32 GB
Test Methodology
Two functionally equivalent modules are written—one with interface{} and one with generics. Each module sorts 10,000 random integers and computes their average. The Go testing package is used to run benchmarks, recording execution time and memory usage.
Sample Code
package main
import (
"errors"
)
func CompareByInterface(a, b interface{}) (bool, error) {
// Check if a and b are of type int
aInt, okA := a.(int)
bInt, okB := b.(int)
if !okA || !okB {
return false, errors.New("not int")
}
return aInt < bInt, nil
}
func CompareByGeneric[T int | float64](a, b T) bool {
return a < b // direct comparison, requires comparable type
} package main
import (
"testing"
)
func BenchmarkCompareByInterface(b *testing.B) {
a := 10
for i := 0; i < b.N; i++ {
_, _ = CompareByInterface(a, b)
}
}
func BenchmarkCompareByGeneric(b *testing.B) {
a := 10
for i := 0; i < b.N; i++ {
_ = CompareByGeneric[int](a, a+1)
}
}Results
Both benchmarks were executed on the same hardware and OS, ensuring comparability.
Interface{} Benchmark
Operations: 1,000,000,000
Average time per operation: 0.1169 ns
Memory allocated per operation: 0 bytes
Allocation count per operation: 0
Generic Benchmark
Operations: 1,000,000,000
Average time per operation: 0.1128 ns
Memory allocated per operation: 0 bytes
Allocation count per operation: 0
Analysis
Performance comparison : The generic version is marginally faster, saving about 0.0016 ns per operation. While the difference is tiny, it could accumulate in extremely high‑iteration scenarios.
Memory behavior : Both implementations show zero heap allocations, indicating that the comparison operations are stack‑only.
Reason for the gain : Generics resolve the concrete type at compile time, avoiding runtime type assertions required by interface{}.
Practical significance : For most applications the nanosecond‑level advantage is negligible, but in performance‑critical loops with billions of iterations, even such a small gain may be worthwhile.
Conclusion
The benchmark demonstrates that Go generics provide a slight performance edge over interface{}, alongside improved type safety and clearer code. While the advantage is modest, it may be relevant for highly optimized, compute‑intensive workloads.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development & AI Practice
DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
