Intuition for `HashMap` vs `BTreeMap` performance

60 points AndrewGazelka

I've been hearing conflicting advice about when to use HashMap vs BTreeMap in Rust.

  • BTreeMap has ordering, which is its main advantage.
  • Some say BTreeMap is good for small sets (< 1000 elements) but gets worse after that.
  • Others claim that for very small sets, even linear search outperforms the binary search that BTreeMap uses (possibly mentioned in https://www.youtube.com/watch?v=DMQ_HcNSOAI), which makes me think even for small sets a HashMap would be better (contradicting previous point).
  • With a fast hasher, maybe performance differences are less significant?

I'm not sure if there's a clear-cut rule for when to use each. Has anyone got some practical insights or intuition?

Some discussions I've found:

Is there really no good intuition for this choice, or am I missing something obvious?