<?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: Luiz Guilherme Pereira</title>
    <description>The latest articles on DEV Community by Luiz Guilherme Pereira (@l2zg7e).</description>
    <link>https://dev.to/l2zg7e</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%2F207655%2Fabe25176-9697-433f-a55c-8fef3861e181.jpeg</url>
      <title>DEV Community: Luiz Guilherme Pereira</title>
      <link>https://dev.to/l2zg7e</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/l2zg7e"/>
    <language>en</language>
    <item>
      <title>WeakMap in Javascript</title>
      <dc:creator>Luiz Guilherme Pereira</dc:creator>
      <pubDate>Wed, 20 Jul 2022 23:45:23 +0000</pubDate>
      <link>https://dev.to/l2zg7e/weakmap-in-javascript-54i3</link>
      <guid>https://dev.to/l2zg7e/weakmap-in-javascript-54i3</guid>
      <description>&lt;p&gt;Recently I've been abusing &lt;code&gt;WeakMap&lt;/code&gt; to solve performance issues in Javascript.&lt;br&gt;
The &lt;code&gt;WeakMap&lt;/code&gt; work in a very similar way to its stronger brother &lt;code&gt;Map&lt;/code&gt;. You provide a key and a value. Whenever you need to retrieve the value for that key, you can get it from the &lt;code&gt;WeakMap&lt;/code&gt;.&lt;br&gt;
The difference between &lt;code&gt;Map&lt;/code&gt; and &lt;code&gt;WeakMap&lt;/code&gt; is that the latter does not hold reference to the key, if the garbage collector needs to clear it from the memory, &lt;code&gt;WeakMap&lt;/code&gt; will say that it is fine, the key and value will be cleared at the same time.&lt;/p&gt;
&lt;h2&gt;
  
  
  Caching
&lt;/h2&gt;

&lt;p&gt;The power this characteristic gives us is that we can start caching things without the risk of causing a memory leak. If the key is ready to be collected, the cache won't be a problem in the way of GC.&lt;/p&gt;

&lt;p&gt;We can implement a higher-order function called &lt;code&gt;weakMemoize&lt;/code&gt; that will decorate our function with a caching layer using a &lt;code&gt;WeakMap&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;weakMemoize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;toDecorate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;WeakMap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;toDecorate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can use to cache our functions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getKeys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;weakMemoize&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;getKeys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// second time the value is read from the cache.&lt;/span&gt;
&lt;span class="nx"&gt;getKeys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm not concerned about the memory consumption of this because whenever &lt;code&gt;obj&lt;/code&gt; or &lt;code&gt;getKeys&lt;/code&gt; go out of scope, the garbage collector will also collect the values in the map. I'm avoiding executing &lt;code&gt;Object.keys&lt;/code&gt; a second time with no extra complexity in the implementation of my function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Considerations
&lt;/h2&gt;

&lt;p&gt;Couple of things to take in consideration. A &lt;code&gt;WeakMap&lt;/code&gt; key can only be an object (&lt;code&gt;object&lt;/code&gt; or &lt;code&gt;Array&lt;/code&gt;), we cannot use a primitive value as key.&lt;br&gt;
Another consideration is that if you have long living objects, caching things like this, might not be so beneficial, a lot of objects will be retained in memory, consider using a &lt;a href="https://www.npmjs.com/package/lru-cache"&gt;LRU&lt;/a&gt; cache.&lt;/p&gt;

&lt;p&gt;Also, you don't need to implement your own &lt;code&gt;weakMemoize&lt;/code&gt;, there are good implementations in the &lt;a href="https://www.npmjs.com/package/@emotion/weak-memoize"&gt;npm&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;br&gt;
Saw a problem in my post? Please comment or reach me out on &lt;a href="https://twitter.com/l2zg7e"&gt;twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>performance</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
