<?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: Srujan Kumar</title>
    <description>The latest articles on DEV Community by Srujan Kumar (@theshadowfax).</description>
    <link>https://dev.to/theshadowfax</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%2F338917%2F68b45db7-d4fb-41bc-9791-374408e660e5.jpeg</url>
      <title>DEV Community: Srujan Kumar</title>
      <link>https://dev.to/theshadowfax</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/theshadowfax"/>
    <language>en</language>
    <item>
      <title>Better recursions with Tail Call Optimization</title>
      <dc:creator>Srujan Kumar</dc:creator>
      <pubDate>Thu, 21 May 2020 22:54:50 +0000</pubDate>
      <link>https://dev.to/theshadowfax/better-recursions-with-tail-call-optimization-3f61</link>
      <guid>https://dev.to/theshadowfax/better-recursions-with-tail-call-optimization-3f61</guid>
      <description>&lt;p&gt;On a beautiful day in the Shire, Bilbo Baggins is learning programming and was practicing recursions.&lt;/p&gt;

&lt;p&gt;He wrote this code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fact = (num) =&amp;gt;{
  if(num === 1) return 1; // non-recursive base case
  return n * fact(n-1); // recursive part
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So he ran it, it worked fine with 3 and 4.&lt;br&gt;
But this curious headed little hobbit wants to check how long can it go.&lt;br&gt;
He gave input of 100000 and&lt;/p&gt;

&lt;p&gt;&lt;code&gt;RangeException:&lt;br&gt;
               Maximum stack size exceeded&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;He ran to seek help from Gandalf, then the wise wizard explained to him how stacks work.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Whenever you call a function then it pushes a new frame on to the stack and once the operation of that frame is done then it is removed from the stack&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So the above code for input "4" would translate into this&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S3fAj97M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7h6orv3b4one0jd7jmoh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S3fAj97M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7h6orv3b4one0jd7jmoh.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the ram has a limited size and it allocates a little part of it whenever a program runs. Keeping this restriction in mind, when you run the same code with input "100000" the stack length increases and eventually reaches a point where it cannot add any new frames into it.&lt;/p&gt;

&lt;p&gt;And now Bilbo asks &lt;code&gt;Master can we not optimize it?&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The Gray one smokes the pipe and says &lt;code&gt;Of course my old friend!&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tail Call Optimization&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;If the last thing a routine does before it returns is call another routine, rather than doing a jump-and-add-stack-frame immediately followed by a pop-stack-frame-and-return-to-caller. &lt;br&gt;
Tail call optimization reduces the space complexity of recursion from O(n) to O(1). Our function would require constant memory for execution. It does so by eliminating the need for having a separate stack frame for every call.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So Gandalf rewrote the code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const fact = (num,acc = 1) =&amp;gt; {
     if(num === 1) return acc;
     return fact(n-1,acc * n);
 }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the stack view looks something like&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a7op3nrb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mdgctpl0d4l6pnoeaa8l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a7op3nrb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mdgctpl0d4l6pnoeaa8l.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here whenever you call the fact function instead of adding a new frame on to stack the frame is removed from the stack since it is the last thing to be done by it.&lt;/p&gt;

</description>
      <category>tco</category>
      <category>javascript</category>
      <category>ts</category>
      <category>optimisation</category>
    </item>
  </channel>
</rss>
