This isn't far off from Elixir's Keyword Lists. They're considered an alternative to hashmaps for key/value stores, and would be equivalent to this JS:
var kw_list = [["key", "value"], ["key2", "value2"]];
What's kind of messed up is they still give you key/value operations that just have to walk the whole list.
These are called alists in scheme and other lisps. They aren't meant to be used with very high cardinality maps but for very small ones they can be faster than regular hashes
Though it's worth mentioning that you really only use them for things like options, where linear access isn't a big deal cuz you have like 10 items in the whole list.
They're also convenient for kv data that you're gonna iterate over anyway
the second is probably faster than the first, in the case with only a two-element map -- assuming comparing two keys for equality is fast, and hashing a key is relatively slow.
Honestly, until you're at lots of items (think 10,000 or more), the list will probably outperform the (presumably) hashmap or tree-based version due to cache coherency.
Real world performance doesn't give a shit about Big O.
45
u/JasonTie Jun 29 '19
This hurts my soul in an indescribable way.
49
u/brakkum Jun 29 '19
Gross
23
u/[deleted] Jun 29 '19
This isn't far off from Elixir's Keyword Lists. They're considered an alternative to hashmaps for key/value stores, and would be equivalent to this JS:
What's kind of messed up is they still give you key/value operations that just have to walk the whole list.
15
u/mnkb99 Jun 30 '19
Love it. Ship it
13
u/[deleted] Jun 29 '19
I hate this
5
u/anydalch Jun 30 '19
the second is probably faster than the first, in the case with only a two-element map -- assuming comparing two keys for equality is fast, and hashing a key is relatively slow.
5
u/[deleted] Jun 30 '19
The output comments below the console statements are wrong in the first code.
Funny! :)
8
u/Dworgi Jun 30 '19
Honestly, until you're at lots of items (think 10,000 or more), the list will probably outperform the (presumably) hashmap or tree-based version due to cache coherency.
Real world performance doesn't give a shit about Big O.
2
u/NickUnrelatedToPost Jun 30 '19
Thats only good for longer lists. For shorter lists, use redis.
1
u/Weatherby42 Jun 30 '19
I kinda lowkey enjoyed looking at this...
1
u/aniruddha0pandey Jun 30 '19
That CS Degree has to go somewhere