|
|
|
|
|
by omginternets
1861 days ago
|
|
Linear search through a slice is almost always going to be faster than a map operation, because N is almost always small and slices have great cache locality. If your slice has a million entries, it's definitely a different story. |
|
Searching through a map is much faster than you think. At 20 entries searching through a slice of strings is already slower than searching in a map. For ints this threshold is 100 entries. Feel free to test on your own machine, my benchmark code is here: https://gist.github.com/JelteF/1251f180966eb974d7732ea6c3d3b...
You can run it with:
go test -bench='.' -benchtime=1s slicemap_test.go
The benchmark code is based on the one from this blogpost: https://www.darkcoding.net/software/go-slice-search-vs-map-l...
After looking at that code though I didn't trust the results too much anymore, because of various issues with it. So I decided to fix those issues.