Skip to main content Skip to sidebar

Hash Speed in Golang

When choosing a hash function in Go, performance is often a crucial factor. This article compares the speed of various hash functions available in Go’s standard library: MD5, SHA1, SHA256, SHA512, FNV-1 and FNV-1a (32/64/128), CRC32, CRC64, and maphash.

Benchmark Setup

All benchmarks were run on an Apple M3 Pro processor using Go’s built-in testing framework. The test data consists of a 100-character string: “The quick brown fox jumps over the lazy dog. This is a sample text for benchmarking various hash functions in Go.”

var testData = []byte("The quick brown fox jumps over the lazy dog. This is a sample text for benchmarking various hash functions in Go.")

Results

Hash FunctionHash SizeTime per OperationMemory per OperationAllocations per Operation
CRC3232-bit26.02 ns/op8 B/op1 allocs/op
CRC6464-bit78.24 ns/op8 B/op1 allocs/op
FNV-1 128128-bit487.5 ns/op24 B/op2 allocs/op
FNV-1 3232-bit131.9 ns/op8 B/op1 allocs/op
FNV-1 6464-bit135.7 ns/op8 B/op1 allocs/op
FNV-1a 128128-bit533.1 ns/op24 B/op2 allocs/op
FNV-1a 3232-bit132.1 ns/op8 B/op1 allocs/op
FNV-1a 6464-bit138.4 ns/op8 B/op1 allocs/op
maphash64-bit16.14 ns/op0 B/op0 allocs/op
MD5128-bit253.6 ns/op16 B/op1 allocs/op
SHA1160-bit98.89 ns/op24 B/op1 allocs/op
SHA256256-bit93.53 ns/op32 B/op1 allocs/op
SHA512512-bit277.4 ns/op64 B/op1 allocs/op

Analysis

Cryptographic Hashes

Among cryptographic hash functions, SHA256 (93.53 ns/op) surprisingly outperforms SHA1 (98.89 ns/op) on Apple Silicon, likely due to hardware acceleration. MD5 (253.6 ns/op) and SHA512 (277.4 ns/op) are the slowest cryptographic options tested.

FNV Hash Family

The FNV hash functions show moderate performance:

  • FNV-1 and FNV-1a variants perform similarly within each bit size
  • 32-bit variants: FNV-1 (131.9 ns/op) vs FNV-1a (132.1 ns/op)
  • 64-bit variants: FNV-1 (135.7 ns/op) vs FNV-1a (138.4 ns/op)
  • 128-bit variants are significantly slower: FNV-1 (487.5 ns/op) vs FNV-1a (533.1 ns/op)
  • All 128-bit variants require 2 allocations instead of 1

FNV-1 vs FNV-1a difference: The main difference between FNV-1 and FNV-1a is the order of operations. FNV-1 multiplies first then XORs, while FNV-1a XORs first then multiplies. FNV-1a generally provides better avalanche characteristics and distribution for short inputs, making it slightly more suitable for hash tables, though both produce high-quality hashes.