In Go switch constant expression with 4 or more cases is implemented as a binary search .
Cases are sorted at compile time, and then binary search.
In this small test, we see that a switch with only 5 cases is on average 1.5 times faster than the corresponding if-then-else sequence. In general, we can assume the difference in O (logN) and O (N) in performance.
3 out of fewer cases are compared linearly, so expect the same performance as if-then-else.
source share