Maps Are A Game Changer For JavaScript

preview_player
Показать описание


🌎 Find Me Here:

#Shorts
Рекомендации по теме
Комментарии
Автор

i can feel his anxiety to not go over time. 💀

shawn-skull
Автор

Maps are useful but this is not a good explanation why. Maps advantages over plain objects are:
- insertion order is preserved
- they can be indexed using any type including objects*
- they provide their own iterators for their keys, values and entries

* The drawback is it implies having a pointer to the key object elsewhere in memory in order to access the value. This isn't mentioned in the video even it's crucial to know.

MDo-wwwm
Автор

Important: If you use objects as keys, they are compared by reference equality. So if you use .get({userId: 1}), you'll get undefined as {userId: 1} is a new object.

antonpieper
Автор

There are trade-offs between standard objects and Map().

Please don't prioritize using Map() over objects. Avoid them unless you need special keys like functions or are just making a hashMap.

Iteration over objects is trivial, whereas Map iteration is... different.

If you do use Map objects (which is fine for data lookup), just understand that Map() is missing for...in functionality. You have to use for...of.

If you wind up using for...in, there won't be an error and your code will 'run' without functioning.

clarkhinchcliff
Автор

Ah yes boy how I love maps with keys as multiple types. More marinara sauce please

puopg
Автор

Important caveat: Map cannot be serialized, so not good for anything you want to persist.

junzhengca
Автор

Apart from performance concerns already raised. I would add degraded code readability at the top, accessing, adding new keys to object-based maps are super-simple 😎

orcwarrior.
Автор

Using an object as key is really bad practice.

Not sure if this is correct for Javascript but in other programming languages you are using maps for performance because they have another search key algorithm.

wolfgang
Автор

I don't think it's a good idea to use an object as a key, if you lost the reference to the object, you can't access the value by key again

sney
Автор

In most of the cases that I use a map I just need to map strings to other things, I don't need to add, change or delete anything, like mapping tailwind classes. In this cases, what do you think is better, the object or this Map? The Map syntax just looks a bit more confusing with the array of arrays

ninguern
Автор

You just explained clearly thanks bro😊

Rishi-xjiy
Автор

for me who works with React, most of the data I manage need to be serialized somehow, and Maps aren't serializable, what makes them useless

edupazz
Автор

Thank you for making all these awesome tutorials I have learned allot from them!

sven
Автор

instead you should look at the docs where the pros and of cons of objects and maps are detailed, then decide which you should use

limenpepper
Автор

An important thing to note is that Map isn’t serializable but plain objects are.

chriszhang
Автор

My favorite use case for maps is being able to map a socket connection to a user session. No more setting IDs on the connection's object and mapping that to a session.

shapelessed
Автор

That object mapping to an object will be an object reference not an object value. Especially since you're using an immediate object that implementation will be completely useless for looking up later.

jonas
Автор

heh, thanks for these shorts. with these I feel less guilty randomly watching shorts on youtube.

well produced too, for the short time-length of shorts.

thanks!

sourisrsourisr
Автор

Next video: Javascript developer discovers warm water.

altus
Автор

you can also do it with normal objects with the variable key syntax { [{obj: 'key'}]: {value: 'obj'} }, but that's pretty rare

marvinma