I need some quick lookup tables and I’m trying to move away from using objects to using maps, reasons are I assume them to be faster and I want to try something I haven’t used before
This is what I usually do:
const obj1: {[id: string]: string[]} = {}
// let's assume we're inside a loop and we procured someId and someOtherId
if (obj1[someId] == null) {
obj1[someId] = [someOtherId]
} else {
obj1[someId].push(someOtherId)
}
And this is where I’m at with the new map trying to do the same as above
const map1 = new Map<string, string[]>()
if (!map1.has(someId)) {
map1.set(someId, [someOtherId])
} else {
map1.set(someId, [...map1.get(someId)!, someOtherId])
}
I can’t understand if I’m doing it wrong or not, it seems wrong, cause I assume map should be faster but having a get inside a set doesn’t seem faster that doing a simple push, I think it should quite common but I found no example about, I ask for the correct way of doing it.
fmj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.