Alagarasan Profile picture
Jan 18, 2023 9 tweets 7 min read Read on X
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 Image
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

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Alagarasan

Alagarasan Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @AlagarasanWDev

Jan 28, 2023
Generator function* in JavaScript:

A thread 🧵

#100DaysOfCode #javascript #Angular #reactjs #devbubble
In JavaScript, generator functions are a special type of function that allows you to use the yield keyword to pause and resume the execution of the function.

#100DaysOfCode #NodeJS #CodeLife
They allow you to create iterators, which are objects that can be used to iterate over a sequence of values.

A generator function is defined using the function* keyword and can be invoked using the yield keyword.

#100daysofcodechallenge #NodeJS #reactjs
Read 9 tweets
Jan 27, 2023
Memoization: Using Proxy in JavaScript

#100DaysOfCode #javascript #rarely_used #devbubble
Memoization is a technique used to optimize the performance of a function by caching its results for a given set of inputs.

#100DaysOfCode #reactjs #Angular #NodeJS
The idea is that if a function is called multiple times with the same input, it's more efficient to return the cached result instead of re-computing the result each time.

#100daysofcodingchallenge #reactjs #Angular #NodeJS
Read 9 tweets
Jan 26, 2023
Data virtualization: With Proxies in JavaScript.

A thread 🧵

#100DaysOfCode #javascript #rarely_used #devbubble
Data virtualization using Proxies in JavaScript refers to the ability to create virtualized data structures, such as virtualized arrays, that load their data on demand rather than loading all data at once.

#100DaysOfCode #ReactJS #Angular
This can be useful for improving the performance of an application when working with large data sets.

In this Eg., we create a VirtualArray class that takes in two args- the length of the array & a loader function.

#100DaysOfCode #javascript #NodeJS #ReactJs #Angular #devbubble Proxy - VirtualArray in JS
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(