<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Guilherme</title>
    <description>The latest articles on DEV Community by Guilherme (@ggm).</description>
    <link>https://dev.to/ggm</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F556104%2F59f9ecd8-8a73-446a-9a14-6db4491e1c11.jpg</url>
      <title>DEV Community: Guilherme</title>
      <link>https://dev.to/ggm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ggm"/>
    <language>en</language>
    <item>
      <title>JavaScript ES6 keyed collections</title>
      <dc:creator>Guilherme</dc:creator>
      <pubDate>Mon, 02 Aug 2021 14:52:32 +0000</pubDate>
      <link>https://dev.to/ggm/javascript-es6-keyed-collections-544n</link>
      <guid>https://dev.to/ggm/javascript-es6-keyed-collections-544n</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;With Data Structures we can store, organize, order and handle data. We need to understand how and when use determinate structures.&lt;/p&gt;

&lt;p&gt;JavaScript has some built-in structures introduced on es6 version, even though these data structures have some time of existence has many developers has doubt about how to use them, today I wanna try to clarify the information about these.&lt;/p&gt;

&lt;h2&gt;
  
  
  Map
&lt;/h2&gt;

&lt;p&gt;Map is an object and works as a common object, the major difference between them is because map lets you work with internal functions to make the insertion, deletion or get one element with a more simplistic form.&lt;/p&gt;

&lt;p&gt;Also, Map only permit a unique key with diferents values. So if I create a map like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const map = new Map();

map.set('first', 1);

console.log(map.get('first')); // 1

map.set('first', 100);

console.log(map.get('first')); // 100

console.log(map.size); // 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can note the value is changed but only one key as stored on our Map.&lt;/p&gt;

&lt;p&gt;Map is iterable, so we can use a for..of or for each to iterate through our structure and make operations there.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const map = new Map();

map.set('first', 1);
map.set('second', 2);

for(let item of map) {
  console.log(item);
}

for(let [key, value] of map.entries()) {
  console.log(key, value);
}

for(let key of map.keys()) {
  console.log(key);
}

for(let value of map.values()) {
  console.log(value);
}

map.forEach((item, key) =&amp;gt; {
  console.log(key, item);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;for...of&lt;/code&gt; each iteration return an array like this &lt;code&gt;[key, value]&lt;/code&gt;, with &lt;code&gt;forEach&lt;/code&gt; on each we have three parameters, first the value, them the key and finally the map itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why/When use Map?
&lt;/h3&gt;

&lt;p&gt;We wanna use Map structure when it's necessary to keep control of information about the object, and we need to keep keys unique, also Map has a simple usage, so it's simple to get used to using.&lt;/p&gt;

&lt;h2&gt;
  
  
  WeakMap
&lt;/h2&gt;

&lt;p&gt;WeakMap is a collection of key/value in which keys are weakly referenced. &lt;/p&gt;

&lt;p&gt;Because keys are weakly referenced, they cannot be enumerated, so we can't iterate them like Map and cannot obtain the keys. &lt;/p&gt;

&lt;p&gt;We can use WeakMaps like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const weakMap = new WeakMap();

const value1 = {};
const value2 = function(){};
const value3 = "I'm the third value";
const value4 = { foo: 'foo' };
const value5 = { key: 'foo' };

weakMap.set(value1, value2);

console.log(weakMap.has(value3)); // false

console.log(weakMap.get(value1)); // Returns the value based on key, in this case function() {}

weakMap.delete(value1);

weakMap.set(value5, value4);

console.log(weakMap.get(value5)); // Using a object that already in memory, we can access the position

weakMap.set({ myKey: 'myKey' }, {value: 1});

console.log(weakMap.get({ myKey: 'myKey' })); // Will return undefined, because the object on function call is one and the object on the set function is another
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: We can't use primitives values like keys with WeakMaps&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why/When use WeakMap?
&lt;/h3&gt;

&lt;p&gt;Some use cases for WeakMaps, &lt;a href="https://stackoverflow.com/questions/29413222/what-are-the-actual-uses-of-es6-weakmap" rel="noopener noreferrer"&gt;here&lt;/a&gt; have some discussion on the topic, here I will put some tests and my understandings about the data structure: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When we need to handle some private data and do not want to iterate that data, only getting the specific property, WeakMap can be a good choice.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Set
&lt;/h2&gt;

&lt;p&gt;Sets are collections that permits the storage of any type of unique values. With sets we can avoid duplicate data, remembering that objects references can be added like a new value too.&lt;/p&gt;

&lt;p&gt;We can use Sets like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const set = new Set();
set.add(1); // set [1]
set.add(5); // set [1, 5]
set.add(5); // 5 already was been setted, so set [1, 5]

const object = { foo: 'foo', bar: 'bar' };
set.add(object); 
set.add({ foo: 'foo', bar: 'bar' }); // This object will be added because is another reference
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's possible check if a value was inserted in our Set:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const set = new Set();

set.add(1);

set.has(1); // true

set.has(5); // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also we can check the size of Sets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const set = new Set();

set.add(1);
set.add(5);
set.add(10);

console.log(set.size) // 3

set.delete(10); // Removes 10 from our set

console.log(set.size); // 2 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like Maps, Sets can also be iterated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const set = new Set();

set.add(1);
set.add("text");
set.add({foo: 'bar', bar: 'foo'});

for(let item of set) {
  console.log(item); 
}

// Keys will have the inserted values
for (let item of set.keys()) {
  console.log(item)
}

/**
 * key and values are the same here
 */
for (let [key, value] of set.entries()) {
  console.log(key);
  console.log(value);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using spread operator we can create a copy of a Set and use as an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const set = new Set();

set.add(1);
set.add("text");
set.add({foo: 'bar', bar: 'foo'});

const setToArray = [...set];

setToArray.forEach(item =&amp;gt; {
  console.log(item);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why/When use Set?
&lt;/h3&gt;

&lt;p&gt;We would like to use Sets when it's necessary to keep unique values without the need to use key/value on our structure. For that Sets are the best choice because they will keep the consistency of our data.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: it's valid to think about the objects references example because even though you pass the same object to the set, it will be saved because are different references.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  WeakSet
&lt;/h2&gt;

&lt;p&gt;WeakSet objects permit you to store weakly held objects. Like as Set collection, WeakSet will permits each object occurrence only once.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What the difference of WeakSet and Set?&lt;/strong&gt; WeakSet only accepts objects, so they cannot contain any values like Sets. Another difference is like the WeakMap, WeakSet has weak references of the objects they held, if no other references of an object store exist this object can be garbage collected. Last but not less important, the WeekMap collection cannot be enumerable.&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; have an interesting example about the usage of that structure.&lt;/p&gt;

&lt;p&gt;Simple example of using WeakSet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const weakSet = new WeakSet();
const foo = {};
const bar = {};

weakSet.add(foo);
weakSet.add(bar);

console.log(weakSet.has(foo)); // true 
console.log(weakSet.has(bar)); // true

weakSet.delete(foo); 

console.log(weakSet.has(foo)); // false    
console.log(weakSet.has(bar)); // true
console.log(weakSet.has({})); // false because is another reference    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Useful links:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Key equality is based on the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#same-value-zero_equality" rel="noopener noreferrer"&gt;sameValueZero&lt;/a&gt; algorithm&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Keyed_collections" rel="noopener noreferrer"&gt;Keyed collections&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects" rel="noopener noreferrer"&gt;Standard built-in objects&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
