WeakMap in JavaScript
A thread 🧵
#100DaysOfCode #javascript #rarely_used
A WeakMap is a map that holds weak references to its keys.
This means that the keys are eligible for garbage collection if there are no other references to them.
This feature can be useful for implementing caches or other similar data structures.
#reactjs #Angular #NodeJS #js
A WeakMap is a special kind of map where keys must be objects and the map doesn't prevent garbage collection of its keys.
You can think of a WeakMap as a map that doesn't "own" its keys, and therefore doesn't keep them alive.
#100daysofcodechallenge #100DaysOfCode
Unlike the built-in Map object, WeakMap keys are weakly held, meaning they can be garbage collected if there are no other references to them.
#learning #LearnAndEarn #js
In this example, we create a new WeakMap and store a value "Hello, World!" associated with the key obj. You can retrieve the value using the get() method.
#100DaysOfCode #reactjs #Angular #nodejs
WeakMap has only a few methods like set, get, has & delete, it doesn't have any size property or iteration like Map.
WeakMap is useful when you want to associate data with an object, but don't want to prevent the object from being garbage collected.
#100daysofcodechallenge #js
For Eg., you can use a WeakMap to store metadata associated with an object, without worrying about keeping the object alive.
It's worth noting that, WeakMap is not a replacement for Map & doesn't have the same capabilities, it's more suitable for specific use cases.
#reactjs
I'll share more real-time examples of WeakMap usage in my upcoming posts. Follow me for more.
#100DaysOfCode #reactjs #NodeJS #Angular #javascript
Let me know in the comments if you have any questions.
If you enjoyed this thread, kindly RT🔁 the first tweet.
This account exists to help you:
- Learn & revamp your web-dev skills
Follow🏃 @AlagarasanWDev ... let's learn together.
That's a wrap!
❤️
Cheers!
#techtwitter
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.
