<?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: vishal1025</title>
    <description>The latest articles on DEV Community by vishal1025 (@vishal1025).</description>
    <link>https://dev.to/vishal1025</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%2F492037%2F8fff1dbe-aabe-4935-9a9d-a6bfa3eeb684.png</url>
      <title>DEV Community: vishal1025</title>
      <link>https://dev.to/vishal1025</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vishal1025"/>
    <language>en</language>
    <item>
      <title>Memoization in JS at function level</title>
      <dc:creator>vishal1025</dc:creator>
      <pubDate>Fri, 16 Oct 2020 18:45:24 +0000</pubDate>
      <link>https://dev.to/vishal1025/memoization-in-js-at-function-level-4gfk</link>
      <guid>https://dev.to/vishal1025/memoization-in-js-at-function-level-4gfk</guid>
      <description>&lt;p&gt;I recently encountered an interesting problem wherein you need to achieve a simple memoization at a function level.&lt;/p&gt;

&lt;p&gt;Explaining the problem first,&lt;/p&gt;

&lt;p&gt;Let's suppose you have been given a function for summation of 2 numbers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add = (a, b) =&amp;gt; {
 return a,b
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You have to write a function let's suppose memoize which receives a function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function memoize(func) {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Input to the problem&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mem = memoize(add);
console.log(mem(1,2)) =&amp;gt; 3
console.log(mem(2,4)) =&amp;gt; 6
console.log(mem(1,2)) =&amp;gt; 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;So, problem is you need to complete the memoize function to return the output but the catch is if the input params are already being computed then you don't need to compute them again&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, let's start revising some core concept of JS i.e every function is ultimately an object in JS,🧐🧐&lt;/p&gt;

&lt;p&gt;Let's think how we can use this concept in our function memoize&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function memoize(func) {

  // Since, we can have a property in objects, right?
  if(!memoize.preInputs) {
    memoize.preInputs = []
  }

return function() {
        for(let i = 0; i &amp;lt; memoize.preInputs.length; i++) {
          if((memoize.preInputs[i]['arg1'] === arguments[0] &amp;amp;&amp;amp; 
            memoize.preInputs[i]['arg2'] === arguments[1]) || 
            (memoize.preInputs[i]['arg1'] === arguments[1] &amp;amp;&amp;amp; 
            memoize.preInputs[i]['arg2'] === arguments[0])) {
            console.log('precomputed');
            return memoize.preInputs[i]['result'];
          } 
         }
         memoize.preInputs.push({
              arg1: arguments[0],
              arg2: arguments[1],
              result: func(arguments[0], arguments[1])
            });
         console.log('newly calculated');
         return memoize.preInputs[memoize.preInputs.length - 1].result;
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's try using the output,&lt;br&gt;
console.log(mem(1,2)) &lt;br&gt;
=&amp;gt; newly calculated&lt;br&gt;
   3&lt;br&gt;
console.log(mem(3,4))&lt;br&gt;
=&amp;gt; newly calculated&lt;br&gt;
   7&lt;br&gt;
console.log(mem(1,2))&lt;br&gt;
=&amp;gt; precomputed&lt;br&gt;
   3&lt;/p&gt;

&lt;p&gt;So, that's it this is a one way by which you can achieve this, I am pretty much sure you can have other ways also&lt;/p&gt;

&lt;p&gt;Bye!&lt;br&gt;
Happy Coding !!😁😁😁&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>angular</category>
    </item>
  </channel>
</rss>
