All Benchmarks

Go Benchmark

Type Assertions

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 atCPUs

1 CPU

Fastest

Type Assertion

0.23 ns/op

Slowest

Reflection Type Of

0.56 ns/op · 2.5x slower

32 CPUs

Fastest

Type Assertion With Ok

0.24 ns/op

Slowest

Reflection Type Of

0.59 ns/op · 2.4x slower

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 (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.5xfaster(148%)thanReflection Type Of
1xfaster(1%)thanType Assertion With Ok
1xfaster(1%)thanType Switch
32 CPUs
2.4xfaster(142%)thanReflection Type Of
1xslower(1%)thanType Assertion With Ok
1xfaster(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 (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.5xfaster(146%)thanReflection Type Of
1xslower(1%)thanType Assertion
32 CPUs
2.4xfaster(141%)thanReflection Type Of
1xslower(1%)thanType Assertion
1xslower(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, returning false instead of panicking on mismatch.

CPU Scaling (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; never panics on mismatch
		v, ok := d.(concreteDoer)
		if ok {
			sinkInt = v.Do()
		}
	}
}
1 CPU
2.4xfaster(145%)thanReflection Type Of
1xslower(1%)thanType Assertion
Same speed asType Switch
32 CPUs
2.4xfaster(144%)thanReflection Type Of
1xfaster(1%)thanType Assertion
1xfaster(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 (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.5xslower(148%)thanType Assertion
2.4xslower(145%)thanType Assertion With Ok
2.5xslower(146%)thanType Switch
32 CPUs
2.4xslower(142%)thanType Assertion
2.4xslower(144%)thanType Assertion With Ok
2.4xslower(141%)thanType Switch

Contributors