ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก

Use Maps more and Objects less

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.พ. 2023
  • More cool things you can do with Maps and Sets, including all the code shown here in this video, in my full blog post: www.builder.io/blog/maps
    #javascript #performance

ความคิดเห็น • 130

  • @Steve8708
    @Steve8708  ปีที่แล้ว +12

    More cool things you can do with Maps and Sets, including all the code shown here in this video, in my full blog post: www.builder.io/blog/maps

  • @zacharyallen4270
    @zacharyallen4270 ปีที่แล้ว +130

    It's worth noting that "frequently" in the terms of the MDN docs means an insane amount - ie orders of magnitudes more than you'd need for most use cases. The inconvenience in using maps, and the gotchas around memory mean that they're usually just a premature optimization and not really a good thing to recommend to a beginner.

    • @rallokkcaz
      @rallokkcaz ปีที่แล้ว +7

      Can confirm this.

    • @TheMorgenTee
      @TheMorgenTee ปีที่แล้ว +13

      I can confirm this. I started using maps, because of a video like this one and ended up having to store the maps to disk after each change. The solution I found for that was to convert the map to an object and store it as json - at which point I decided to use an object instead.

    • @amans6504
      @amans6504 ปีที่แล้ว +9

      Why we introduce beginners everywhere? Infact it's better to teach the right thing to beginners from the starting

    • @zacharyallen4270
      @zacharyallen4270 ปีที่แล้ว +1

      @@amans6504 the issue is using maps is almost never the right thing in js.

    • @devcoffee
      @devcoffee ปีที่แล้ว +7

      These are some odd takes. Outside the context of a beginner, this is a well explained. Most intermediate/advanced developers are using Typescript anyways and I’ve found typings with Maps and Sets to be much more intuitive. Plus, the ability to use a reference value as a key on a Map/WeakMap has saved me a tremendous amount of headache. I do agree, that in some instances it’s just easier to use objects but throwing Maps and Sets into the mix is a good call. It’s like this, if I only ever know about a hammer, I’m going to use a hammer for everything. But what about the screwdriver and table saw?

  • @sanjarcode
    @sanjarcode ปีที่แล้ว +39

    This is a very good way to present content, especially for more experienced devs. Subscribed.

    • @luciusrex
      @luciusrex 7 หลายเดือนก่อน

      even for beginners. its only been a week of programming for me, started with js. and ive been using array of ids so i can reference them to an array of objects with a .find(). i might rewrite some of my code to include maps and sets.

  • @zenner24
    @zenner24 ปีที่แล้ว +4

    it is amazing how you make these videos better and better with each and every one, thank you

  • @bjul
    @bjul ปีที่แล้ว +19

    Awesome stuff Steve! So happy I've found this channel. Great format, presentation and topics. Keep it up!

  • @muhammadfitrie7061
    @muhammadfitrie7061 ปีที่แล้ว +1

    One of the best channel for JavaScript!

  • @angusbosmans
    @angusbosmans ปีที่แล้ว +4

    Very well explained and good examples too!

  • @997Pancakes
    @997Pancakes ปีที่แล้ว +1

    This is extremely useful content. I have not come across anything similar elsewhere. Keep it up.

  • @syrix5914
    @syrix5914 ปีที่แล้ว

    I read the blog earlier this week, and now seeing this again is pretty cool.

  • @talatkuyuk6556
    @talatkuyuk6556 ปีที่แล้ว +7

    Your way of explanation is excellent. Precise and focused. Thanks...

  • @bassam.2023
    @bassam.2023 11 หลายเดือนก่อน

    Useful explanations, thanks!

  • @just_ppe
    @just_ppe ปีที่แล้ว +10

    It's good to remember that maps are not JSON.stringified by default...
    Also, the order of keys is the insertion order (so it's good for stuff like LRU cache)

  • @mohammadjavadaghazadehfard7873
    @mohammadjavadaghazadehfard7873 ปีที่แล้ว

    Hi Steve, Thank you🌹🌹. It was very cool. Please make more videos like this. Good luck.🙏🙏

  • @Hendrixdh
    @Hendrixdh ปีที่แล้ว +1

    This is great stuff, thank you.

  • @greob
    @greob ปีที่แล้ว

    Nice video, nicely edited. Thanks for sharing!

  • @codesymphony
    @codesymphony ปีที่แล้ว +8

    What I took away from it: Objects should be used to describe actual things (ie. real life objects, hence oop), maps are used as a collection of things where you need more functionality than an array

  • @sck3570
    @sck3570 ปีที่แล้ว

    An awesome explanation and presentation

  • @ricardoestrella95
    @ricardoestrella95 ปีที่แล้ว +2

    Just found accidentally your channel and based on the number of likes, I think you are way underestimated. Awesome videos, new subscriber here.

  • @93420
    @93420 ปีที่แล้ว

    I am new in this channel and this video is really nice thanks :)

  • @Felipekimst
    @Felipekimst ปีที่แล้ว

    Never thought I could learn that much in 10min

  • @MrKaspi097
    @MrKaspi097 ปีที่แล้ว

    Nice! Very usefull video. Keep it up!

  • @LePhenixGD
    @LePhenixGD ปีที่แล้ว

    Man your video came at the perfect moment!

  • @mo_still_alive
    @mo_still_alive ปีที่แล้ว

    thank you, thank you, thank you 🙏 for such a content

  • @nikbivation
    @nikbivation ปีที่แล้ว

    this is gold! Subbed

  • @andriim1440
    @andriim1440 ปีที่แล้ว

    thanks for so helpful video

  • @codesymphony
    @codesymphony ปีที่แล้ว

    Great stuff

  • @abhishekchary416
    @abhishekchary416 ปีที่แล้ว

    Pretty Impressed by the content you make. Have a lot to learn, know and implement from your videos. Keep posting best practices. Tried to connect over LinkedIn too but asking email.

  • @mfkfawas5157
    @mfkfawas5157 ปีที่แล้ว

    awesome content

  • @chubbycat9212
    @chubbycat9212 ปีที่แล้ว

    Really Cool

  • @gooo1762
    @gooo1762 5 หลายเดือนก่อน

    Thanks!

  • @Blast-Forward
    @Blast-Forward ปีที่แล้ว

    That metadata use case blew my mind.

  • @scraper1000
    @scraper1000 ปีที่แล้ว

    This is great content. Subscribing for more!

  • @ofeenee
    @ofeenee ปีที่แล้ว +1

    Great format, content at a perfect pace! Thank you.
    Question: Sets don’t maintain order?

  • @perishableq1020
    @perishableq1020 ปีที่แล้ว

    very nice video

  • @ala.garbaa
    @ala.garbaa ปีที่แล้ว

    Amazing keep going

  • @isaahliu
    @isaahliu 7 หลายเดือนก่อน

    quality content.

  • @patricknelson
    @patricknelson ปีที่แล้ว +2

    That metadata use case is brilliant. Knew about the arbitrary keys but didn’t think of that approach.

  • @firedforfighting
    @firedforfighting ปีที่แล้ว

    I love learning all these new things!! I have only known the one way for converting map to json with
    const mapAsJSON = JSON.stringify(Object.fromEntries(someMap)) now I know about the replacer!
    from JSON back to map with const mapFromJSON = new Map(Object.entries(JSON.parse(someJSON)))

  • @__renesan
    @__renesan ปีที่แล้ว

    Gracias

  • @danser_theplayer01
    @danser_theplayer01 ปีที่แล้ว +1

    Yeah maps are cool cuz "1" and 1 can be two different keys, I think you can even use an object as a key.

  • @SuperQuwertz
    @SuperQuwertz 9 หลายเดือนก่อน

    Nice

  • @ViruSamah
    @ViruSamah ปีที่แล้ว +1

    Legend

  • @dearlordylord
    @dearlordylord ปีที่แล้ว

    > maps would preserve the order of keys
    It's crucial to specify that the kind or order is the original insertion order of the keys. I will only rely on this a little though, as it could harm readability: nobody expects this semantics, but only the people who have read and do remember this particular behaviour. It's not normal in other languages, either!

  • @shaneaudinett8464
    @shaneaudinett8464 ปีที่แล้ว

    Excellent format, very informing, and yet simple. I am an intermediate professional developer and because of your tutorial, I will be using sets and maps immediately in my projects! Thank you and keep up the good work!

    • @yegorzakharov8514
      @yegorzakharov8514 ปีที่แล้ว +1

      Mate, if you are truly intermediate, you should have the foresight to do more research and run your own tests before shoving maps in your projects.

    • @shaneaudinett8464
      @shaneaudinett8464 ปีที่แล้ว

      @@yegorzakharov8514 that's obvious though isn't it? Using in the projects where they are necessary?

    • @yegorzakharov8514
      @yegorzakharov8514 ปีที่แล้ว

      @@shaneaudinett8464 well it should be. Just sounded in the first comment that you saw a few min video and will now start replacing most of your object literals to maps. That would not be smart. And if its personal projects, thats one thing. If you did that at work, very diff story

    • @shaneaudinett8464
      @shaneaudinett8464 ปีที่แล้ว

      @@yegorzakharov8514 that's called assuming. Thanks for taking a personal interest in my life internet stranger 😉

    • @yegorzakharov8514
      @yegorzakharov8514 ปีที่แล้ว

      @@shaneaudinett8464 tbh I wasn't trying to be a dick. Just often see that many junior developers see something on tiktok then begin to religiously use it without knowing what it is, and often making their things worse without realising

  • @dmz985
    @dmz985 ปีที่แล้ว

    Sounds like pizza has arrived for the end, good timing from delivery guy

    • @Steve8708
      @Steve8708  ปีที่แล้ว

      I was hoping no one would notice 😂

  • @hicoop
    @hicoop ปีที่แล้ว +12

    Personally, I like to use objects for making object-based constants. The simple syntax of creating and fetching data is just a lot smoother than maps. Because these objects are constants and lookup speed isn't really a priority, I'll probably stick with objects

    • @codesymphony
      @codesymphony ปีที่แล้ว +2

      it's not one or the other lol both have use cases

    • @Microphunktv-jb3kj
      @Microphunktv-jb3kj ปีที่แล้ว +5

      if speed becomes an issue.. why are you even using JavaSript facepalm....

    • @ccgarciab
      @ccgarciab ปีที่แล้ว

      ​@@Microphunktv-jb3kj js can be performant enough for many applications on the web without the need for wasm, and offers a better suited API for accessing the browser's functionality. But just like any other language, you can kill your good enough performance.

  • @mitchelldirt
    @mitchelldirt ปีที่แล้ว +2

    Didn’t even know about maps before I saw your blog post and then subsequently this video 😳

  • @KonstantinShutkin
    @KonstantinShutkin ปีที่แล้ว +1

    if Map preserves order of keys than Set do this as well according to spec notes: "For iteration purposes, a Set appears similar to a Map where each entry has the same value for its key and value."

    • @IvanKleshnin
      @IvanKleshnin 5 หลายเดือนก่อน

      Objects preserve key orders as well, check newer ES spec. The video contains wrong information.

  • @juniordevmedia
    @juniordevmedia ปีที่แล้ว

    gotcha!

  • @static-san
    @static-san ปีที่แล้ว

    One thing that would make Maps and Sets more useful is if they supported the [] operator like Objects do. I've been told this can be done by doing something clever in the prototype; however, I haven't been able to figure out how.

  • @_the_one_1
    @_the_one_1 ปีที่แล้ว +3

    Does it provide the TypeScript support the object does?

  • @ftnsco
    @ftnsco ปีที่แล้ว

    cool. how would you use it in react? Whould you store it outside component or in a context? Thanks.

  • @javascriptforeveryhing
    @javascriptforeveryhing ปีที่แล้ว

    Bro, You are super quick before graps

  • @mildlymusical4054
    @mildlymusical4054 ปีที่แล้ว

    hey amazing content , would like to know how it was edited, if you could shed some light on this , that would be great

  • @frontend_ko
    @frontend_ko ปีที่แล้ว

    Great

  • @vinit095
    @vinit095 7 หลายเดือนก่อน

    I've read somewhere that creating an object is slower than using other approaches

  • @thejcedeno
    @thejcedeno ปีที่แล้ว

    How did ya record and edit this? I like the format

  • @Famouzi
    @Famouzi ปีที่แล้ว

    What do you think of keepin maps inside useState?

  • @axelb423
    @axelb423 8 หลายเดือนก่อน

    Map is interesting, but when we need to add an entry between two existing keys, we have to create a complete new map and that is not cool for complexe entries with nested elements.

  • @muhammadhasnat9835
    @muhammadhasnat9835 ปีที่แล้ว

    Waiting for your frontend course bro

  • @dawidwraga
    @dawidwraga ปีที่แล้ว +1

    Can a map where key = ID and value = object replace arrays of objects in context of data fetching and state management in react? For example, arrays of objects are fetched from database, serialised using the methods descripted in this video, sent to the front end as maps, and stored / manipulated in state? Or would this be bad practice?

    • @niksonkanyuka4323
      @niksonkanyuka4323 ปีที่แล้ว

      Had same question in mind watching this video

    • @dawidwraga
      @dawidwraga ปีที่แล้ว +1

      @@niksonkanyuka4323 after some research it seems that using arrays of objects should be used over maps in react state due to mutability

  • @Johnny-rn8fb
    @Johnny-rn8fb ปีที่แล้ว

    Gold

  • @Chalisque
    @Chalisque ปีที่แล้ว +4

    Worth noting that if using objects and arrays as keys, this is done according to object IDENTITY: consider this transcript from node
    > m = new Map()
    Map(0) {}
    // Use object as a key
    > b = { a: 42 }
    { a: 42 }
    > m.set(b,42)
    Map(1) { { a: 42 } => 42 }
    > m.get(b)
    42
    // what if we change that object?
    > b.c = 43
    43
    > m.get(b)
    42
    // what if we make a copy of that object?
    > c = {...b}
    { a: 42, c: 43 }
    > m.get(c)
    undefined
    // try similar with an array as a key
    > m.set([1,2],50)
    Map(2) { { a: 42, c: 43 } => 42, [ 1, 2 ] => 50 }
    > r = [1,2]
    [ 1, 2 ]
    > m.get(r)
    undefined
    > m.get([1,2])
    undefined
    so if we store using a literal array or object as a key, we have a leak, and if we lose access to the object itself, we cannot recreate the key.

  • @khaledoghli3400
    @khaledoghli3400 ปีที่แล้ว

    Hi Steve,
    Why you don't make course, your tips or information it's most advanced someone tell us about it.
    many Thanks .

  • @mrlectus
    @mrlectus ปีที่แล้ว

    How would this work with react

  • @Meleeman011
    @Meleeman011 7 หลายเดือนก่อน

    Maps are ok, but i think i'll continue to use objects and arrays

  • @ChamplooMusashi
    @ChamplooMusashi ปีที่แล้ว

    you should really start with a disclaimer on maps - espescially since you're highlighting the gotchas of objects

  • @patrikzita5246
    @patrikzita5246 ปีที่แล้ว +2

    Interesting video... I would also like to know if it's okay to use MAPS in React in some situations, or if it makes sense to use it for communication with a database. Thanks for your answer.

  • @Tbagstealer
    @Tbagstealer ปีที่แล้ว

    I like the idea of Map but I really wish there was an easy way to perform transforms. The Map transform is so janky in it's current state. Maybe ES7 will address this but it's just bad DX at this point

  • @user-hy5cx9iu3l
    @user-hy5cx9iu3l ปีที่แล้ว

    In most cases I don't need all that Map functionality.

  • @climatechangedoesntbargain9140
    @climatechangedoesntbargain9140 ปีที่แล้ว

    even with ngrx?

  • @Htbaa
    @Htbaa ปีที่แล้ว +1

    After doing web development for a good 20 years I must say I’m ashamed to admit I didn’t even know JavaScript has a Map… Would probably have been different if I would’ve written JS on the backend…

  • @SurenKhorenyan
    @SurenKhorenyan ปีที่แล้ว +1

    Wow, cool. I've been using JS for years and I didn't know about that. Thank you!
    And how do you do subtitles like this?

  • @user-gs3lm7gt5e
    @user-gs3lm7gt5e ปีที่แล้ว

    I am curious. At my job I encounter many situation where I need to do type checking, either using type guard or discrimination check or else, is map better at checking types over objects. This is one of the reasons why I am getting frustrated with javascript... :(

    • @dealloc
      @dealloc ปีที่แล้ว +1

      Depends on your use case. Maps (and Sets) are immutable, always so you cannot guarantee values to be defined for given keys. It requires runtime checks to avoid bugs.
      Objects on the other hand, can be defined as a readonly value and narrow its type using `as const` (and the new `satisfies` operator). This is useful in cases where you have constant values that you know are always available.

  • @YusufSalahAdDin
    @YusufSalahAdDin 11 หลายเดือนก่อน

    Hey there, it seems you missed writing the " like trivially implementing an O(1) LRU Cache", is it normal?
    Thank you

  • @akam9919
    @akam9919 5 หลายเดือนก่อน

    How did I never know structuredClone was a function?!?!?!?!?!?!!?!!!!!!!!

  • @Blast-Forward
    @Blast-Forward ปีที่แล้ว

    Why do you need WeakMaps? To not have to clean up yourself?

  • @eqprog
    @eqprog ปีที่แล้ว +1

    WeakMaps have one big drawback compared to maps which is implied in this video but not explicitly stated: WeakMap keys must be Objects, they cannot be primitives.
    Edit: Not trying to be critical. I’m really liking your videos so far. I appreciate having more realistic examples over what MDN provides

  • @Sindoku
    @Sindoku 8 หลายเดือนก่อน

    Does this mean maps are pretty much always better?

  • @bloodjopa
    @bloodjopa ปีที่แล้ว

    One more video for the sake of video

  • @well.8395
    @well.8395 ปีที่แล้ว

    This channel is an example of "I don't know shit, but I'll recommend shit to everyone because I just learnt this". No, don't use maps as a drop in replacement for objects.

  • @blackpurple9163
    @blackpurple9163 ปีที่แล้ว +1

    So i should use Maps more than Objects in react?

    • @tabiasgeehuman
      @tabiasgeehuman ปีที่แล้ว

      Depends on your use case. If you are frequently adding and removing from an object, yes. Else, no.

    • @blackpurple9163
      @blackpurple9163 ปีที่แล้ว +3

      @@tabiasgeehuman I'm talking about with useState and similar hooks

    • @tabiasgeehuman
      @tabiasgeehuman ปีที่แล้ว +1

      @@blackpurple9163 I don't use react that much, but I don't think objects Vs maps affect it since setState takes in a whole new object to my knowledge

    • @blackpurple9163
      @blackpurple9163 ปีที่แล้ว

      @@tabiasgeehuman yes it creates a new object/array by spreading and updating the previous values, so it's always returning a new object/array, so in that case should I use a Map instead (if it's possible)?

    • @tabiasgeehuman
      @tabiasgeehuman ปีที่แล้ว

      @@blackpurple9163 you shouldn't since Map is good for mutating an existing, not for creating/destroying new maps.
      Again, note that I have limited knowledge on how react works.

  • @rafaaferid1789
    @rafaaferid1789 ปีที่แล้ว +1

    But here you are talking about mutating the objects.
    In functional programming case (for example in React), mutating the object is already a bad practice.
    So I think objects are still easier to work with in that case (unless you want to copy the Map each time and edit the new copy).
    Also, true, deleting keys is still better with Maps.

  • @kashishsharma713
    @kashishsharma713 ปีที่แล้ว +1

    First like, bruh

  • @jxn22
    @jxn22 ปีที่แล้ว +1

    I do not like the fixed CC in this video.

    • @preservedmoose
      @preservedmoose 4 หลายเดือนก่อน

      Agreed, in a foreign language for me too (I speak English, not USA English)

  • @joshmanhuwa2475
    @joshmanhuwa2475 ปีที่แล้ว

    bruh just make a school for JavaScript, i would totally buy your courses

  • @Thogor
    @Thogor ปีที่แล้ว +1

    This is good information if you're working in a context where you have extreme performance requirements. For the other 95% of usecases, this is a really bad recommendation and even an antipattern akin to stringly typed code.

  • @Chris-jm2gx
    @Chris-jm2gx ปีที่แล้ว

    These are good to know, but I can smell premature optimization.

  • @wizamit
    @wizamit 8 หลายเดือนก่อน

    Content is good. Title is bad.

  • @MatthewKennedyUK
    @MatthewKennedyUK ปีที่แล้ว +1

    Just don’t use JavaScript.

    • @zakuguriin4521
      @zakuguriin4521 3 หลายเดือนก่อน

      Why not? JavaScript is pretty powerful.

  • @hosseinpanahy5358
    @hosseinpanahy5358 ปีที่แล้ว +1

    Your method of teaching ain't lucid and clear

  • @TsoiIzAlive
    @TsoiIzAlive ปีที่แล้ว

    Using map everywhere is gonna cost performance

  • @BartekKozera
    @BartekKozera ปีที่แล้ว

    This is a bad suggestion, performance will probably never be your problem, prototype will definitely never be a problem and working with Maps is way more clunky. Please don't do it

  • @vinit095
    @vinit095 7 หลายเดือนก่อน

    I've read somewhere that creating an object is slower than using other approaches