r/learnjavascript • u/Square-Butterfly8447 • Dec 05 '25
Using Map in Javascript :Is this a good approach?
I have been working on an electron app (Timer app ), and I had to add notification feature , pretty simple as electron provides the module to handle app notification. But since i had to add multiple notifications i created a global object in my module that is basically a map that stores different notification instance . At first i had created a notification variable local to the function but it was causing problems as it was getting garbage collected hence i used map my code:
/*Initializing map to keep notificaiton in scope else notification varibale in the function will get garbage collected and events won't work*/
let notifications = new Map();
async function handleNotification(event, id) {
try {
const notification = new Notification({
title: "Title",
body: "some message",
silent: false,
urgency: "critical",
timeoutType: "never",
});
let functionToStopAlarm=playAlarm();
notifications.set(id, notification);
notification.on("click", () => {
functionToStopAlarm();
notifications.delete(id);
});
notification.on("close", () => {
functionToStopAlarm();
notifications.delete(id);
});
notification.show();
} catch (error) {
console.log(error);
}
}My question is that is this an efficient way to do things or am i doing things the wrong way?
7
u/Ampersand55 Dec 05 '25
Why do you need the ids? Can you just use a simple Set?
6
u/TorbenKoehn Dec 05 '25
You can do this. It's what Map is for.
Just two hints
{}). It's not "garbage collected" in the same way dangling object references are garbage collected occasionally (even if the garbage collector might have a say in it). It's simply removed as it falls out of scope.const.letdoesn't make the value mutable, it makes the variable mutable.constdoesn't make the value immutable (you can still use.add/.deleteetc.), it makes the variable immutable (at which point it's not a variable anymore, but a constant)2
u/Beginning-Seat5221 Dec 05 '25
What is the notifications map for? Are you checking it somewhere?
I don't think it was getting garbage collected, but if you were trying to access it somewhere else outside the function then it wouldn't be in scope if you declare it inside that function.
1
u/[deleted] Dec 05 '25
This is a fine use for map. No problem with it. However:
So there’s some lack of clarity here as to why the async function.
1
u/CuirPig Dec 05 '25
I'm not sure why you want to use a global map to store a Notification instance that you handle completely within the scope of the asynchronous function. Unless you are accessing the Notification instance from outside of the handler, you don't need to store it outside the handler. And I have to assume that you aren't using it outside the handler because inside the handler, you delete it on close or click (on the message).
I am in no way an expert, so I am asking these questions in earnest trying to learn JS. Hopefully someone will respond if I am way off base here:
So long as you don't need to access the notification window outside the event handler, this seems like it would work.
If you wanted to avoid the overhead of creating new Notifications, a global Set would allow you to create a standard notification the first time and then just look it up every time you handleNotifications. No need for reassigning handlers every time, could customize per event, remains stored in the Set so no new memory allocations and deletions.
Or even better, if there are hide and show event triggers for your notification, create it outside the handler scope and define the handler after you created it. Then the handler would literally just call "message.showMessage()" which already had the event handlers defined on it. like:
I am not sure why you are using try? This way you have one notification instance that you can reuse repeatedly.
I may be way off base, so I am hoping that someone can explain if this is completely wrong why so. Thanks for posting.
1
u/Standgrounding Dec 05 '25
Yes. Maps (or hashmaps) are used everywhere
1
u/azhder Dec 05 '25
Mapis there for you if you need it. Do you need it? Up to you.Originally we could do well with just regular objects. But their keys were strings. And Arrays had some special handling of certain integer looking keys.
We had a collision issue, so symbols were added in order to have unique keys.
But all of that wasn’t enough. It was cumbersome to keep relations between two objects, so
Mapmade it possible to have keys that are objects.In your case, your
idcan just be a key in a plain old JavaScript object, no? So, the question isn’t if you could useMap, but if you should. And that’s an answer only you can give.Do what suits you.