Theo has some really interesting videos, but the more I dig into the archive the more I find little gems like this. It's the definition of a quick tip, and it helps explain Maps and Sets in JavaScript (and their advantages) way better than anything I've seen before. The video ends with a hopeful statement that the viewer can maybe think of a few times they've used Objects or Arrays where a Map or Set would have been better and oh boy, yes I can 😂
Key takeaways:
- Use a Map for any kind of "object-like" data that you need to edit, particularly if those edits include adding or removing items. The example given is a collection of Users that are themselves data objects, but a Map gives you a much quicker way to reference specific keys and modify them, or get/set users from that list.
- Use a Set for data arrays that need to be unique (Sets automatically remove duplicates) or whether you need to quickly add or delete values, as there are native functions for both of those operations that are more performant than looping through an array to find/replace/add data.