This is probably more of an opinion based question. But recently on a project I have seen that Map
is always used over array
.
My question is when would you ever use an array
over a Map
?
Note I mean in cases where an array would work as well as a Map.
i.e. storing data, checking it exists, and deleting it.
example here:
// Array
const teams = [];
if (teams.includes(team.id)) {
return;
}
teams.push(team.id);
// do stuff
teams = teams.filter(
(id) => id !== team.id
);
// Map
const teams = new Map();
if (teams.has(team.id)) {
return;
}
teams.set(team.id, team.id);
// do stuff
teams(team.id);
As I understand Map is more performant, you also get methods like get
, set
, delete
which are useful.
If faced with the task above what would people use and why?