Map vs WeakMap

Issue with Object

				
					//************************Issue with Object******************************************************/

//**Objects as keys -- The issue --We only have the last key---->*/

const object = {};
const name1 = {};
object[name1] = "Anurag";
console.log(object[name1]);

const name2 = {};
object[name2] = "Abhishek";
console.log(object);

//OUTPUT:->
/*
we have only Abhishek but not Anurag now
{[object Object]: 'Abhishek'}
*/
				
			

Map

				
					//**1.Unlike Objects we will have both the names----------------------->*/
const map = new Map();
console.log("name1", name1);

map.set(name1, "Anurag");
map.set(name2, "Abhishek");
console.log(map);

//OUTPUT:-
/*
Map(2) {{…} => 'Anurag', {…} => 'Abhishek'}
[[Entries]]
0: {Object => "Anurag"}
key: {}
value: "Anurag"
1: {Object => "Abhishek"}
key: {}
value: "Abhishek"
size: 2
*/

//**2.It will ovveride the already existing value-------------------->*/
map.set(name1, "Rahul");
console.log("map ovveridden::->", map);

/*
Map(2) {{…} => 'Rahul', {…} => 'Abhishek'}
[[Entries]]
0: {Object => "Rahul"}
key: {}
value: "Rahul"
1: {Object => "Abhishek"}
key: {}
value: "Abhishek"
size: 2
*/

//**3.delete to delete specific item in map---------------------------------->*/

//**4.to loop in objects I need to loop using for in loop,but I can use for of in Map----------------->*/

for (let [key, value] of map.entries()) {
  console.log(`key is :: ${key}--value is :: ${value}`);
}
//OUTPUT:-
// key is :: [object Object]--value is :: Rahul
// key is :: [object Object]--value is :: Abhishek

//**5.convert map to array--------------------------------------------------->*/
//convert map to array
const array1 = [...map];
console.log(array1);

// (2) [Array(2), Array(2)]
// 0: Array(2)
// 0: {}
// 1: "Rahul"
// length: 2
// [[Prototype]]: Array(0)
// 1: Array(2)
// 0: {}
// 1: "Abhishek"
// length: 2

				
			

WeakMap

				
					//************************WEAK MAP******************************************************/

//1.Weakmap --> you cannot store primitive data types-------------------------->

let wMap1 = new WeakMap();
// wmap.set(1);
//Uncaught TypeError: Invalid value used as weak map key

//**2.Weakmap --> you can store only reference types---------------------------->*/
const w2 = {};
let wMap2 = new WeakMap();
wMap2.set(w2, "Anurag");
console.log("wMap2", wMap2);

//OUTPUT
/*
WeakMap {{…} => 'Anurag'}
[[Entries]]
0: {Object => "Anurag"}
key: {}
value: "Anurag"
[[Prototype]]: WeakMap
*/

wMap2.set(w2, "Abhishek");
console.log("wMap2", wMap2);

//**w2 gets updated from Anurag to Abhishek.---------------------------->*/
/*
WeakMap {{…} => 'Abhishek'}
[[Entries]]
0: {Object => "Abhishek"}
key: {}
value: "Abhishek"
[[Prototype]]: WeakMap */

//**3.Weakmap --> has and delete method.. you cant iterate , no size property ................*/
console.log(wMap2.has(w2)); //true
console.log(wMap2.delete(w2)); //true
console.log(wMap2.has(w2)); //false
				
			

MAP vs WEAKMAP - Few Questions

				
					//************************MAP vs WEAKMAP - Few Questions ********************************/

//******WHAT WILL BE THE OUTPUT OF THE BELOW CODE??********/
var o5 = { name: "Anurag" };
var o6 = { name: "Abhishek" };

var map5 = new Map();
var wMap6 = new WeakMap();

map5.set(o5, "Anurag");
wMap6.set(o6, "Abhishek");

console.log(map5.get(o5)); //Anurag
console.log(wMap6.get(o6)); //Abhishek

//******WHAT WILL BE THE OUTPUT OF THE BELOW CODE??********/

var o5 = { name: "Anurag" };
var o6 = { name: "Abhishek" };

var map5 = new Map();
var wMap6 = new WeakMap();

map5.set(o5, "Anurag");
wMap6.set(o6, "Abhishek");

o5 = {};
o6 = {};

console.log(map5.get(o5)); //undefined
console.log(wMap6.get(o6)); //undefined

//you can still loop on the keys of map....
//And weakMap as you know it is not iterable...
				
			

Leave a Comment