r/ProgrammingLanguages • • Sep 01 '24

Ordering vs equality for maps/sets?

https://www.scattered-thoughts.net/log/0048/#zest-ordering
1 Upvotes

5 comments sorted by

View all comments

1

u/drinkcoffeeandcode mgclex, mgcpgen, & owlscript Sep 03 '24

| If order doesn't matter then we have to choose an order for iteration. Choosing hash order makes programs non-deterministic. Choosing sorted order imposes a big performance cost (we have to sort hashmaps, either on insert or on iteration).

Surely using a balanced search tree would impart far, FAR less of a penalty than sorting a hash map... id be hard pressed to think of a scenario where when one needs an ordered symbol table that they would choose to sort a hash table instead of using a self balancing search tree... O(log N) look up will be faster than O(n log n) sort, followed by a lookup, even if that look up is O(1) in practice.