I'm trying to find an efficient way to store pairs key:value pairs so that you can fast find the other value (while both are unique).
for example: lets say we wanna store pairs "unique_id: email"... and I want to be able to get the other value from either of the two values. What is the less resource consuming (both RAM and calc) way to do it?
Saving memory solution:
const data = {"1": "user1@email.com",
"2": "user2@email.com",
"3": "user3@email.com"}
var email1 = data['1']; //fast research
var user1 = data.find("user1@email.com") //slow research algorithm (find is some function that search through the item... probably in a more inefficient way than the above index-research.
efficient research solution?
const emails = {"1": "user1@email.com",
"2": "user2@email.com",
"3": "user3@email.com"}
const ids = { "user1@email.com": "1",
"user2@email.com": "2",
"user3@email.com": "3" }
var email1 = emails['1'];
var id1 = ids[ "user1@email.com"];
Now... in the second case we are consuming more memory, in the first we have a less efficient research (or is there some algorithm that is as fast as index-research?). For my application I actually have more than 2 unique values (like -and is just an example-id,gmail,microsoftid,passport_no). Of course with the second solution I'm using way more memory, and for my application memory usage is crucial.
So, what is the right way to do this? Is there some kind of js class that does this?
You could use a vector of key-values, so it will be O(n), both for a key and a value.
But native object has O(1) and O(n), so it isn't a big deal.
You could also make a ternary tree, when the left is both less than, the right is both greater than, middle is one is greater, the other is less (or 4ry tree, for better search), but dunno how to balance it well.