WeakSet vs WeakMap in JavaScript Last Updated : 20 Jun, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In JavaScript, there are two types of references strong and weak. The WeakSet and WeakMap are called weak references. Since these are weak references they do not prevent garbage collection if they are the only reference to the object in the memory. These objects are rarely used but are useful when we want memory usage to be automatically managed. JavaScript WeakSet: It is used to store a collection of objects similar to that of set the only difference is that these values are only object and not some particular data type. Syntax: new WeakSet(object)Example: In this example, we will create a new WeakSet object and add elements to it using inbuilt methods. JavaScript var x = new WeakSet(); var y = new WeakSet(null); x.add({}); x.add({}); console.log(x); console.log(y); Output: WeakSet {{…}, {…}} WeakSet {}JavaScript WeakMap: In JavaScript, the WeakMap is used to store value in a key-value pair but it is different as the entries are weakly referred to. The key should always be a JavaScript object but the value can be any primitive JavaScript data type. Example: In this example, we will create a weakmap object and add elements to it. JavaScript function myGeeks() { var looseMap = new WeakMap(); looseMap.set({}, "Ram"); looseMap.set({}, "Raj"); looseMap.set({}, "Rahul"); console.log(looseMap); } myGeeks(); Output: WeakMap {{…} => 'Raj', {…} => 'Rahul', {…} => 'Ram'}What to use? WeakMap is used to create a map-like structure so it is used when data is to be stored in key-value pairs and since it is a weak reference we only use it when data is to be automatically deleted from memory when it is the only reference remaining. We use WeakSet when we want to have unique values in our collection of objects. WeakSet is useful as it behaves like a set and like WeakMap it also has a weak reference. WeakSetWeakMapIt is a collection of unique objects onlyIt is a collection of key-value pairsSet is one-dimensionalMap is two-dimensionalValues can be accessed using keysValues can be accessed using inbuilt methodsKeys are strictly objects only and value can be primitive data type It can take only objects as values and no other primitive data type Comment More infoAdvertise with us Next Article JavaScript WeakMap S shobhit_sharma Follow Improve Article Tags : JavaScript Web Technologies Web Technologies - Difference Between Similar Reads Set vs Map in JavaScript In JavaScript, Set and Map are two types of objects that are used for storing data in an ordered manner. Both these data structures are used to store distinct types of data inside the same object. In Maps, the data is stored as a key-value pair whereas in Set data is a single collection of values th 2 min read JavaScript WeakMap A WeakMap in JavaScript is a collection of key-value pairs where the keys are objects, and the values can be any arbitrary value. Unlike regular maps, WeakMap keys are weakly referenced, meaning they don't prevent garbage collection.The keys in a WeakMap can be automatically removed when no longer i 5 min read JavaScript WeakSet A WeakSet in JavaScript is a collection of unique objects where the values are weakly referenced. It works similarly to a Set, but the objects stored in a WeakSet can be garbage-collected when no longer in use.A WeakSet can only store objects, not primitive values like strings or numbers.Objects in 4 min read JavaScript weakSet add() Method Javascript weakSet.add() is used to add an object at the end of the object WeakSet. The WeakSet object lets you store weakly held objects in a collection. Syntax: weakSet.add(A);Parameters: This method accepts a single parameter value. value: This value will be added to the weakset object. Return Va 2 min read JavaScript weakSet has() Method JavaScript weakSet.has() method is used to return a boolean value indicating whether an object is present in a weakSet or not. The WeakSet object lets you store weakly held objects in a collection. Syntax: weakSet.has(value); Parameters: This method accepts a single parameter value. value: This valu 2 min read Map vs Object in JavaScript In JavaScript, both Map and Object store key-value pairs.Maps offer better performance for frequent additions or deletions. For read heavy operations where strings are keys, Objects provide better performance. Object allows only Strings and Symbols as keys, but Map maintains key order and allows any 4 min read Like