JavaScript Maps & WeakMaps

  Рет қаралды 48,894

techsith

techsith

6 жыл бұрын

ES6 Introduced new Data Structures Map and WeakMap in JavaScript. Understand difference between JavaScript objects and Maps.
Article and code samples from this tutorial
* techsith.com\
Please be my patreons on patreaon
* / techsith
Follow me for technology updates
* / techsith
* / techsith1
Help me translate this video.
* kzfaq.info_vide...
Note: use translate.google.com/ to translate this video to your language. Let me know once you do that so i can give you credit. Thank you in advance.

Пікірлер: 113
@yousufkhan2909
@yousufkhan2909 3 жыл бұрын
TO say thank you will not do justice with your teaching. I have never learnt coding easily before watching your videos. You are a great mentor. Keep it up.
@Techsithtube
@Techsithtube 3 жыл бұрын
Wow, thank you Rahel! Thanks for watching!
@Cazineer
@Cazineer 6 жыл бұрын
Good tutorial. What would be great is if you explained not only what Maps and WeakMaps are, but when and why you would use them over a traditional object.
@paymankhayree8552
@paymankhayree8552 4 жыл бұрын
one reason: you can not loop over an Object
@shyam5631
@shyam5631 3 жыл бұрын
@@paymankhayree8552 No, You can loop over an object.
@shyam5631
@shyam5631 3 жыл бұрын
The main reason is that you can only store keys as strings in javascript object. But in maps we can store the keys of any data types.
@alinawaz4034
@alinawaz4034 5 жыл бұрын
0:50 every time i watch your video when you say *"techsith tutorials"* it feels me like that you are saying *"Taxi Tutorials"* and then i smile :)
@Techsithtube
@Techsithtube 5 жыл бұрын
lol . google translation translate to texi tutorials. :)
@arjunsonip
@arjunsonip 3 жыл бұрын
@@Techsithtube sexy tutorial
@hamzaiqbal725
@hamzaiqbal725 6 жыл бұрын
Man you just explain things really simply.. loved you're videos ❤
@jaganramanikanth9578
@jaganramanikanth9578 5 жыл бұрын
Hi bro, last 3 days I watching ur interview solve question in js and ur all video. It's really helpful, yesterday I attend the interview in CTS, it very helpful for me , keep going bro...
@raghavdinesh3645
@raghavdinesh3645 6 жыл бұрын
Genuine efforts. Thanks
@luthermaria1021
@luthermaria1021 4 жыл бұрын
You are a Genius , there is so many materials out there but just make it easy...Thank you
@duxingsky
@duxingsky 5 жыл бұрын
2:44 JavaScript Object not support any Object as key. It parse an Object toString(), and treat '[object Object]' as key
@saragossaxyxy1577
@saragossaxyxy1577 3 жыл бұрын
but that would be no longer an object but a string.
@FidelGuajardo
@FidelGuajardo 3 жыл бұрын
Very nice and simple example at the end about the difference between a map and a weakmap. This helped me tremendously to understand. Thank you
@VinothKumar5403
@VinothKumar5403 6 жыл бұрын
awesome tutorial again..you are a genius.
@laxmiprajapati2192
@laxmiprajapati2192 6 жыл бұрын
you explain in very simple way which make easy to understand.Thanks a lot
@Techsithtube
@Techsithtube 6 жыл бұрын
Thanks for watching laxmi!
@ErnestGWilsonII
@ErnestGWilsonII 6 жыл бұрын
Another great video as always! Thank you for taking the time to make this and share it with all of us! Near the end of the video, you had a small homework assignment, I want to take a guess as to why the value remained... I suspect when it was initially created using var the value got attached to the global space, additionally I think when creating the map or the weak map using const or let will keep the value contained inside the function scope. Please let us know if you post an answer to the question you raised. As always thumbs up and I am subscribed with notifications turned on, I don't want to miss any videos from your channel!
@Techsithtube
@Techsithtube 6 жыл бұрын
Thanks for subscibing to my channel . I will try to make good videos. :)
@aaaaaaa596
@aaaaaaa596 6 жыл бұрын
Again a masterpiece !!!!
@MylesGmail
@MylesGmail 5 жыл бұрын
TY!
@landgemukesh4171
@landgemukesh4171 5 жыл бұрын
Answer to you question: x was not garbage collected , it will stays there until console refreshed or you forcefully delete it from map
@NaveenRawat51
@NaveenRawat51 5 жыл бұрын
liked as always before watching :-)
@shakilahmed4532
@shakilahmed4532 6 жыл бұрын
soo good, please carry on ..
@monalithakre602
@monalithakre602 Жыл бұрын
It helps me a lot to learn js concept thanks for this amazing videos.
@chandanpradhan449
@chandanpradhan449 6 жыл бұрын
thanks, please make a video related to how dispatch works in react-redux
@ArtemNikolaev
@ArtemNikolaev 3 жыл бұрын
thx, very good explanation
@rekhanshpanchal6142
@rekhanshpanchal6142 5 жыл бұрын
Good explanation.
@debasishpothal2414
@debasishpothal2414 5 жыл бұрын
its nice that we can make object a key but my question is why would anyone ever wanna do that ?
@gyohza
@gyohza 5 жыл бұрын
I have a question: I see that it's interesting that maps allow us to pair an object to any other value. That is cool and all, but why would I do that if I can simply assign the value as a property of the object I want to pair it with? The only reason I can think of is to free the object of the value in question in case I ever want to iterate through it with a for in loop or something. Also you mentioned you would like your videos translated to other languages. I'm about to resume classes at college right now, but I might try to give you translated transcripts in Portuguese (Brazilian) of a few videos every now and then, with the only problem being syncing everything (I have no experience in this). Would unsynced transcripts be of any help?
@iliashterev38
@iliashterev38 4 жыл бұрын
I have said many time - Thank you and God bless you. What I would criticize here is that in practice you would have a simple (primitive usually) key and perhaps object value. Your examples were the other way around and that gave me a vertical head spin :) . Also I came to learn what those WeakMaps are and why I would need them and yet I got nothing except that they do not hold garbage collected keys in weird example.
@d33psp33d
@d33psp33d 3 жыл бұрын
this guy rocks!
@justjavascript4920
@justjavascript4920 3 жыл бұрын
This is good :)
@creativeuk6882
@creativeuk6882 6 жыл бұрын
The one thing that I like about you is the detail of your tutorials. I cannot figure out how you learn these things with such details. Can you tell what resources you use to learn these things. Thanks for the video SIR
@Techsithtube
@Techsithtube 6 жыл бұрын
Lot of my knowledge comes from Mozilla developers network . Which is usually complex to understand . I do go through lots of other resource to simplify things.
@narayanam8242
@narayanam8242 5 жыл бұрын
WeakMaps will not retain entry because it only saves the reference where as Map actually saves a copy of the key object and so it retains even after GC. Please correct if am wrong.
@totaltotal157
@totaltotal157 4 жыл бұрын
This might look like it's the same as setting props on an Object but what ends up happening is you get a hash conflict. toString is used to create the key which will result in two entries with the same key ( [object Object] ). Then when you retrieve it has to iterate over all entries with this key and find the proper entry by strict equality. What this is means is that in a way of saying map.get(obj) is now an O(n) operation compared to O(1) in the case of accessing an object unless V8 internally sets the address of the object as key which would make sense. Also the reason you don't get a key in there is because the object gets garbage collected so the reference is gone. If you declare the object with var, global will keep a reference to it.
@abhijeetdey
@abhijeetdey Жыл бұрын
Thank you
@prashansapriya5963
@prashansapriya5963 2 жыл бұрын
Good tutorial
@seenuvasanv
@seenuvasanv 6 жыл бұрын
Thanks
@ganeshshetty8441
@ganeshshetty8441 2 жыл бұрын
for of is available with regular object as well , but we will have ti iterate through Object.entries(obj)
@chanduiit42
@chanduiit42 6 жыл бұрын
Hello @techsith.. I have read in a Medium article that keys are always coerced to strings in javascript So the object is converted into [object Object]. Even when we declare another object as a key it converts this into [object Object] and confuses to override the old one.
@roshnikutty4252
@roshnikutty4252 6 жыл бұрын
Great video! Thank you! Just curious on your question in the end, I am assuming that the Array memory space created during map does not exist when you change to weakmap, so it shows the updated entries. But when you start with weakmap on a new browser, it does not allocate memory space for something that does not exist, and so you don't see entries. Just a guess. Please correct me if this is wrong, would be great to know the correct answer.
@Techsithtube
@Techsithtube 6 жыл бұрын
@Roshini Yes, you are somewhat right on that. The culprit is jsfiddle, its holds the environment as a closure. So you have to be bit careful when you use jsfiddle for few cases.
@davidestrada3791
@davidestrada3791 6 жыл бұрын
I would love to understand how Javascript uses 'Garbage collection' and how to write more efficient code / debug garbage collection. Thank you for your well explained videos!
@davidestrada3791
@davidestrada3791 6 жыл бұрын
BTW, I've looked all over youtube for a good video on this and haven't found any useful videos
@RahulKumar-tg5zb
@RahulKumar-tg5zb 5 жыл бұрын
Where we use this. Plz answer
@PawanPatel-mv9rt
@PawanPatel-mv9rt 2 жыл бұрын
I tried with this example but I am still getting the entries when I console log weakmap ut of block
@junaidchoudhary9110
@junaidchoudhary9110 6 жыл бұрын
A suggestion, please make real world projects using js it would be really helpful to follow along for newbie dev. Thanks..:)
@Techsithtube
@Techsithtube 6 жыл бұрын
Yes , thanks for the suggestion . that would be very useful.
@Godwin335
@Godwin335 6 жыл бұрын
Can you please give a real world scenario where Maps are useful?
@kamoroso94
@kamoroso94 5 жыл бұрын
You could use a Map to count frequencies of objects in an array possibly. Also good for a graph data structure. Any time you're using an object literal as a map, use Map instead.
@rupjuly1985
@rupjuly1985 4 жыл бұрын
Thanks buddy
@Techsithtube
@Techsithtube 4 жыл бұрын
Thanks for watching Rupendra :)
@sumitmobiotics3161
@sumitmobiotics3161 4 жыл бұрын
awesome explanation sir.
@Techsithtube
@Techsithtube 4 жыл бұрын
Thanks for watching Sumit.
@sumitmobiotics3161
@sumitmobiotics3161 4 жыл бұрын
@@Techsithtube thanks for creating such good videos, my many js concepts clear from your channel, i am remembering you created one video regrading js clousers , that is best video ever in my life.
@JurajPecháč
@JurajPecháč 6 жыл бұрын
You can use console.table for array.
@Techsithtube
@Techsithtube 6 жыл бұрын
Yes good point.
@Ghaleon
@Ghaleon 4 жыл бұрын
10:10 to prevent this. CAn't we just use LET instead of VAR?
@shyam5631
@shyam5631 3 жыл бұрын
I'm very late to your channel but the answer to the last part is that the previous entry will still be in memory when we run it.
@masibhai339
@masibhai339 5 жыл бұрын
What is the difference between for in loop and for of loop ??
@mukundpatel1
@mukundpatel1 6 жыл бұрын
It's nice, I have one topic, symbol() can you please explain it.
@hardikdesai
@hardikdesai 6 жыл бұрын
Good video to learn on map and weakMap. Regarding the question at the end, I think Map creates the copy of the object passed in key. So even when we update the value of the key object same is not getting reflected inside map key. Whereas WeakMap stores the key with referencing the object passed as key, so if its scope of object is not present it also clears the entries associated with it. But not sure why its retaining the value even after refreshing the browser. Can you please guide on this.
@Techsithtube
@Techsithtube 6 жыл бұрын
If you fefresh the browser it will run the code again . that is why you are seeing it.
@AC-lq2pz
@AC-lq2pz 6 жыл бұрын
Its still in memory, because JS engine not taken the key in garbage collection array. Restart the browser create a new cycle of garbage collection. Please correct me if I'm wrong :)
@Techsithtube
@Techsithtube 6 жыл бұрын
That is correct.
@shubhamsingh-xk4nf
@shubhamsingh-xk4nf 5 жыл бұрын
if it is still in the memory then why it is not visible in the entries while doing console.log ?
@stephane4487
@stephane4487 2 жыл бұрын
Hello, interesting video, but I guess I am still missing a concrete example of when would need to store an object or an array as a key?
@spacewad8745
@spacewad8745 6 жыл бұрын
Sets and WeakSets please!
@alishagupta1157
@alishagupta1157 5 жыл бұрын
{let y1 = {a:[1,2]}; var x1 = new WeakMap(); x1.set(y1,'a')}console.log(x1) I am still getting same output as in map. not working as mentioned in example.
@abhinemo22
@abhinemo22 5 жыл бұрын
Try closing the browser and open it again
@prashanthkumar9352
@prashanthkumar9352 5 жыл бұрын
Hi, Thanks for the explanation, I have a question what is the use of having an object as a key?
@Techsithtube
@Techsithtube 5 жыл бұрын
Lets say you are tracking all the api responses and how many times each different response comes back you do that by adding it to a map as a key and value can be a number that you increment
@genovo
@genovo 2 жыл бұрын
@@Techsithtube is this a way of comparing technically different objects , but when they are keys? So it will see each separate object that matches {first: "John"} response as "equal" to all other {first: "John"} API responses, and match that object key, thus allowing an incremental count?
@narayanam8242
@narayanam8242 5 жыл бұрын
How JS Maps differentiate between key objects? Will it use hash method like in java?
@Techsithtube
@Techsithtube 5 жыл бұрын
Yes I believe it does use the hash method.
@GirishBalanagu
@GirishBalanagu 5 жыл бұрын
Still not understand... why we use 'Object' as a key in Map.
@muzahid00009999
@muzahid00009999 3 жыл бұрын
good but ... if you use value of a to alfa , the tutorial would be more interesting... i am a newbie so its so hard to distinguish which a is your var and which a is your object and which a is your key.....LOL...(lot of love)sir
@eswararaoravada2895
@eswararaoravada2895 5 жыл бұрын
what if? set same object as a key multiple times var map = new Map(); map.set( { name : 'Eswar' } , '100'); console.log(map.has( { name : 'Eswar' } )); //false Ideally it should be TRUE, may be i am checking with different objects but both objects contain same content. How can i solve this, In java we can handle with heshcode() & equels()
@Techsithtube
@Techsithtube 5 жыл бұрын
Its object reference issue. Each object has its own reference so reference of two different objects are never going to be Same
@GameSpreePH
@GameSpreePH 6 жыл бұрын
Great vids. Could you please make regex as well? ;)
@Techsithtube
@Techsithtube 6 жыл бұрын
Sure I would make it. :) Maybe in a week or two .
@464sravan
@464sravan 6 жыл бұрын
what IDE are you using ? is it JSbin ?
@Techsithtube
@Techsithtube 6 жыл бұрын
its JS fiddle
@shaunpx1
@shaunpx1 3 жыл бұрын
Answer to your question at the end: WeakMap() the lexical scope of JavaScript plays a part in browser cache. once the IIFE is executed, the key and the value of that key are removed from memory because the key is taken out of scope.
@mohdsahil226
@mohdsahil226 5 жыл бұрын
Thank you so much for the information. I have one question if you could please answer You said, "for-in" loop is bad. Would you please tell us why it is bad?
@rahulacharya596
@rahulacharya596 3 жыл бұрын
Everything in js is a object(prototypal inheritance), when you use for of loop, it does index based loop, where accessing is faster, but for in loop considers array as an object, so accessing is a little slower..
@fdc_8507
@fdc_8507 6 жыл бұрын
I guess the answer is because the variable x in used by or have a reference to Map so WeakMap not working ... is that right ? :)
@Techsithtube
@Techsithtube 6 жыл бұрын
Yes correct.
@unfathomable1100
@unfathomable1100 6 жыл бұрын
Why am i not able to run the for..of and array spread example on chrome console ?
@Techsithtube
@Techsithtube 6 жыл бұрын
Can you send me the exact code that you are trying to run in chrome console.
@unfathomable1100
@unfathomable1100 6 жыл бұрын
var a = {} var b = { num: 1 } testMap.set(a,'a').set(b,'b').set(a,'c') for(var [key, value] of testMap.entries()){ console.log(key, value) } now it works! Might be some GC issues, i saw values were adding up, instead of being replaced. cleared map, gc'ed values. That fixed it up.
@Techsithtube
@Techsithtube 6 жыл бұрын
oh ok . cool.
@mocococo2877
@mocococo2877 3 жыл бұрын
I think you started with a very twisted example for key. You could start with simple numbers as keys and then extend in more twisted directions.
@RhoTrepaan
@RhoTrepaan 2 жыл бұрын
To me, it looked like key and value were switched in the Map() example
@janaks7016
@janaks7016 5 жыл бұрын
Please do video for JavaScript data structures
@Techsithtube
@Techsithtube 5 жыл бұрын
I will sure to make tutorials on JavaScript data structures.
@josh1529
@josh1529 5 жыл бұрын
@@Techsithtube much awaiting for this ...
@karengyuzalyan7068
@karengyuzalyan7068 3 жыл бұрын
I couldn't find an answer to the HM question :\
@velmurugan-jo3vb
@velmurugan-jo3vb 6 жыл бұрын
Yes, react-redux please...
@Techsithtube
@Techsithtube 6 жыл бұрын
Sure I will focus on React Redux next. :)
@mrlight891
@mrlight891 6 жыл бұрын
Maybe it's cached?
@Techsithtube
@Techsithtube 6 жыл бұрын
Yes, It is somehow cached. I think its more to do with jsfiddle's internals.
@imornar
@imornar 5 жыл бұрын
Dont think so. JS engines needs some time to garbage collect those vales. If you use new browser, there is nothing to clean up..
@concept007
@concept007 5 жыл бұрын
Would you ever consider doing a tutorial for making a 2d game engine?
@Techsithtube
@Techsithtube 5 жыл бұрын
never thought about it. Like unity engine?
@concept007
@concept007 5 жыл бұрын
@@Techsithtube Moreso from scratch. I love you vids you're pretty much my javascript sensei. :D Thats why i'd like to see you tackle making a game engine with javascript. i think it would be pretty epic. Have a wonderful day and thank you for uploading so many helpful videos.
@artemipatev9456
@artemipatev9456 6 жыл бұрын
React redux would be awesome...
@Techsithtube
@Techsithtube 6 жыл бұрын
Sure I will focus on React Redux next. :)
JavaScript Sets and Weaksets | ES2015
14:59
techsith
Рет қаралды 17 М.
Maps vs. Objects in JavaScript - What's the Difference?
11:48
Дибала против вратаря Легенды
00:33
Mr. Oleynik
Рет қаралды 4,8 МЛН
Did you believe it was real? #tiktok
00:25
Анастасия Тарасова
Рет қаралды 46 МЛН
Array.from  Method in JavaScript
12:09
techsith
Рет қаралды 37 М.
Map, Set, WeakMap & WeakSet in JavaScript - Real use case
12:20
How to Use the New JS Object GroupBy Method
18:17
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 3,4 М.
Weak References, Reachability, WeakSets, and WeakMaps
15:54
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 3,4 М.
Tricky JavaScript interview questions and answers
21:01
techsith
Рет қаралды 132 М.
How to use WeakMap and WeakSet in JavaScript
7:02
Andrew Burgess
Рет қаралды 3,3 М.
Async Await JavaScript ES7
26:39
techsith
Рет қаралды 120 М.
Choose a phone for your mom
0:20
ChooseGift
Рет қаралды 5 МЛН
Gizli Apple Watch Özelliği😱
0:14
Safak Novruz
Рет қаралды 6 МЛН