All Benchmarks

Type Assertions — Go Benchmark

Comparing type assertion, type switch, reflection, and comma-ok checks on interfaces.

type-assertiontype-switchreflectioninterface

Compares four ways to check or extract the concrete type behind an interface value: direct type assertion (v.(Type)), a type switch (switch v.(type)), reflection via reflect.TypeOf, and the safe comma-ok assertion (v, ok := v.(Type)). These patterns appear frequently in generic code and interface-heavy designs, and their performance trade-offs are worth understanding.

linux/amd64AMD Ryzen 9 9950X3D 16-Core Processorbenchmarks/type-assertions
Compare at
CPUs
Performance Comparison (lower is better)
CPU:
1

Type Assertion

Fastest (1 CPU)

Direct type assertion using v.(Type). The fastest form but panics if the interface value does not hold the expected type.

CPU Scaling — Type Assertion (lower is better)
// Doer is a minimal interface for benchmarking type assertion mechanisms.
type Doer interface {
	Do() int
}

type concreteDoer struct{}

func (concreteDoer) Do() int { return 42 }

func BenchmarkTypeAssertion_run(b *testing.B) {
	var d Doer = concreteDoer{}
	for i := 0; i < b.N; i++ {
		// Direct type assertion — panics if the type doesn't match
		v := d.(concreteDoer)
		sinkInt = v.Do()
	}
}
1 CPU
2.5×faster(148%)thanReflection Type Of
1×faster(1%)thanType Assertion With Ok
1×faster(1%)thanType Switch
32 CPUs
2.4×faster(142%)thanReflection Type Of
1×slower(1%)thanType Assertion With Ok
1×faster(1%)thanType Switch
2

Type Switch

Type switch using switch v.(type). Allows matching against multiple concrete types in a single construct with pattern-matching semantics.

CPU Scaling — Type Switch (lower is better)
// Doer is a minimal interface for benchmarking type assertion mechanisms.
type Doer interface {
	Do() int
}

type concreteDoer struct{}

func (concreteDoer) Do() int { return 42 }

func BenchmarkTypeSwitch_run(b *testing.B) {
	var d Doer = concreteDoer{}
	for i := 0; i < b.N; i++ {
		// Type switch to determine the concrete type
		switch v := d.(type) {
		case concreteDoer:
			sinkInt = v.Do()
		}
	}
}
1 CPU
2.5×faster(146%)thanReflection Type Of
1×slower(1%)thanType Assertion
32 CPUs
2.4×faster(141%)thanReflection Type Of
1×slower(1%)thanType Assertion
1×slower(1%)thanType Assertion With Ok
3

Type Assertion With Ok

Fastest (32 CPUs)

Comma-ok type assertion using v, ok := v.(Type). Safe alternative to a bare assertion — returns false instead of panicking on mismatch.

CPU Scaling — Type Assertion With Ok (lower is better)
// Doer is a minimal interface for benchmarking type assertion mechanisms.
type Doer interface {
	Do() int
}

type concreteDoer struct{}

func (concreteDoer) Do() int { return 42 }

func BenchmarkTypeAssertionWithOk_run(b *testing.B) {
	var d Doer = concreteDoer{}
	for i := 0; i < b.N; i++ {
		// Comma-ok assertion — safe, never panics on mismatch
		v, ok := d.(concreteDoer)
		if ok {
			sinkInt = v.Do()
		}
	}
}
1 CPU
2.4×faster(145%)thanReflection Type Of
1×slower(1%)thanType Assertion
Same speed asType Switch
32 CPUs
2.4×faster(144%)thanReflection Type Of
1×faster(1%)thanType Assertion
1×faster(1%)thanType Switch
4

Reflection Type Of

Slowest

Reflection-based check using reflect.TypeOf. Most flexible but carries the overhead of the reflect package. The target type is cached before the hot loop.

CPU Scaling — Reflection Type Of (lower is better)
// Doer is a minimal interface for benchmarking type assertion mechanisms.
type Doer interface {
	Do() int
}

type concreteDoer struct{}

func (concreteDoer) Do() int { return 42 }

func BenchmarkReflectionTypeOf_run(b *testing.B) {
	var d Doer = concreteDoer{}
	target := reflect.TypeOf(concreteDoer{})
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		// Reflection-based type check using reflect.TypeOf
		if reflect.TypeOf(d) == target {
			sinkInt = d.Do()
		}
	}
}
1 CPU
2.5×slower(148%)thanType Assertion
2.4×slower(145%)thanType Assertion With Ok
2.5×slower(146%)thanType Switch
32 CPUs
2.4×slower(142%)thanType Assertion
2.4×slower(144%)thanType Assertion With Ok
2.4×slower(141%)thanType Switch

Contributors