Objects are not the only approach for a map data structures in Javascript #shorts

preview_player
Показать описание
Talking about using the Map class in javascript

My VSCode Extensions:
- theme: material community high contrast
- fonts: Menlo, Monaco, 'Courier New', monospace
- errors: Error Lens
- extra git help: Git Lens
- tailwind css intellisense
- indent rainbow
- material icon theme
- prettier & eslint
- ES7+ React Snippets

------------

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

So the alternative to Objects is just another object, well played

rafaelmesaglio
Автор

If you do the true false thing, it's better to use a set along with has(). Less space by discarding booleans plus same speed (constant time) since set is a map under the hood

SogMosee
Автор

Good job leaving out how much more memory this will take up, and not to mention one of the only actually applicable reasons to use a map over an object is the speed you could add remove an iterate through map compared to an object but that’s obviously only because of how much more memory it uses

dabbopabblo
Автор

This wasn't the best example to explain to people the benefits of a Map. The data structure is great for dealing with non-string keys. You could show an example with numeric user IDs as a key, and iterating through those values with the .keys() iterator.

ciaran
Автор

It also remembers the insertion order of the key value pairs.

shivaaddanki
Автор

Amazing short you're good at these programming tip shorts

crsaele
Автор

Nowadays, I just like your videos before I watch it cause I already know its going to be a good one. 👍

ravenMK_
Автор

is it bcoz of any kind of extension he uses which helps him to show errors as you type ?

sahadpop
Автор

The video doesn't mention the security risks with using objects as maps so heres one of them with an imaginary example.


Lets say you have an object called authStatus which keeps track of whether the user is currently logged in or not.

You receive a key, value pair from user, where key is the user's id and the value is a boolean indicating whether the user is logged in or not, So you're adding the pair to an object like so, authStatus[key] = value.

On the other end, you have some admin utility which sends you the current state of the authStatus by calling toString() on it. Assume you have overriden the existing toString with your own.

All well and good, seems like a normal setup, until a user decides have "toString" as their username, since you're using the username directly as the key you would do something like this

authStatus["toString"] = value

And bam your toString method is now gone and your admin utility is broken. This is why Map is more suitable.

This was a silly example, I could've thought of something more real world, but I hope it gets the point accross that you can simply override prototype methods.

wlockuz
Автор

What type of map implementation is it under the hood?

ttrss
Автор

I believe you can do
Let someMap = new Map([["somekey", some Value], ["somekey2", someValur2]] )

elmalleable
Автор

How will you loop over if the object has more values pairs ?

Snapchk
Автор

I often use maps to associate data with DOM elements without having to use weird expando properties on the element. For instance you can have a class Foo with a class property instances and do Foo.instances.get(el)

truvc
Автор

This is hashmap. When working with hashmap, remember that it’s not json, so if you need to send the data somewhere else, you should consider array like below

const hasPaid = [‘id1’, ‘id2’]

Alternatively, convert hashmap to json before sending is fine too but it takes 1 more step. Hashmap is good for handling hundreds of thousands of records but rarely useful in daily code. Remember that hashmap processes small data much slower than an array.

andyvu
Автор

How can we convert the Map to json that can be sent through API? Or objects that we can pass into a Nosql database like Firebase?

crim-son
Автор

What would be useful is just having to make 2 lists, one for true and one for false

utterdonkey
Автор

What do you mean you can overwrite things if you use an object?

spaghettiking
Автор

Using Map() and set() together doesn't consumes more memory than a "simple" object? 🤔

neevs
Автор

And why is this better? Has it predefined Map-related methods?

chennebicken
Автор

I really love your videos sir, but can you please slow down when explaining? Thanks so much

annm
welcome to shbcf.ru