What is the best bet when a scala Map is created and used in a single thread? (Like the java analogous best bet on StringBuffer compared with StringBuilder for constructing strings).
The kind of constraints to choose the Map are:
I investigated
The test showed that there is no clear winner with 50000 keys. I found some
Nevertheless the question is what is the safest bet in general on such circumstances and why?
If your map is not extremely tiny and not huge, and your key is a String
, then collection.mutable.AnyRefMap
is a good bet. collection.mutable.LongMap
is even faster if you can have Long
keys. The reason they exist is precisely to be fast for common use cases.
If most maps are incredibly tiny (0-4 elements), then LinkedHashMap
tends to be best because it avoids the overhead of a hash table. (Immutable maps are also not bad when 4 or fewer elements.)
If maps are really huge (100s of millions of key/value pairs) then the standard collection.mutable.HashMap
is the way to go because performance degrades slightly more gracefully as you run out of space for separate keys.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With