<?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: Abhilash</title>
    <description>The latest articles on DEV Community by Abhilash (@abhilashiam).</description>
    <link>https://dev.to/abhilashiam</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%2F346654%2F26f95484-50c0-48d8-ac60-c97beed7d02f.jpg</url>
      <title>DEV Community: Abhilash</title>
      <link>https://dev.to/abhilashiam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhilashiam"/>
    <language>en</language>
    <item>
      <title>Measure execution time of function in javascript</title>
      <dc:creator>Abhilash</dc:creator>
      <pubDate>Thu, 14 Jan 2021 21:37:51 +0000</pubDate>
      <link>https://dev.to/abhilashiam/measure-execution-time-of-function-in-javascript-3pff</link>
      <guid>https://dev.to/abhilashiam/measure-execution-time-of-function-in-javascript-3pff</guid>
      <description>&lt;p&gt;As we thrive to build performant and scalable applications, profiling and measuring execution time becomes an important part of development.&lt;br&gt;
Often, we need to locate the piece of code that is taking most of the time in the execution stack.&lt;/p&gt;

&lt;p&gt;Though there are different ways to measure execution time in javascript, I struggled to find something customizable.&lt;br&gt;
Here's an attempt to create a simple yet customizable function that measures the execution time of given functions.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


</description>
    </item>
    <item>
      <title>Garbage Collection in Javascript</title>
      <dc:creator>Abhilash</dc:creator>
      <pubDate>Fri, 27 Mar 2020 12:48:14 +0000</pubDate>
      <link>https://dev.to/abhilashiam/garbage-collection-in-javascript-126a</link>
      <guid>https://dev.to/abhilashiam/garbage-collection-in-javascript-126a</guid>
      <description>&lt;p&gt;We shall today discuss memory management and garbage collection in JavaScript.Even though in JavaScript we are not performing any memory operations explicitly, however, it is good to know how it works.&lt;/p&gt;

&lt;p&gt;In the low-level languages like C, programmers need to manually allocate and deallocate the memory using the malloc(), calloc(), realloc(), and free() methods.&lt;/p&gt;

&lt;p&gt;In high-level languages like Java and JavaScript, programmers don't need to explicitly allocate or release memory.JavaScript memory is allocated when things are created (objects, Strings, etc.) and freed automatically when they are no longer used. This process is called Garbage collection.&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;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// allocates memory for a number&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;b&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;1&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// allocates memory for an object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;When the allocated memory is no longer needed, then it will be released.&lt;br&gt;
&lt;em&gt;Memory leaks&lt;/em&gt;, and most memory-related issues, occur while releasing memory.&lt;/p&gt;
&lt;h1&gt;
  
  
  Garbage Collection:
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Garbage collection&lt;/em&gt; is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage or memory occupied by objects that are no longer in use by the program.&lt;br&gt;
We shall look into two algorithms used for garbage collection:-&lt;/p&gt;

&lt;p&gt;1). &lt;strong&gt;Reference-counting garbage collection&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Reference Counting is a programming technique of storing the number of references, pointers or handles to a resource, such as an object, a block of memory, disk space, and others.&lt;/p&gt;

&lt;p&gt;In garbage collection algorithms, reference counts may be used to deallocate objects which are no longer needed. If in reference counting algorithms, there are no references to an object, it will be automatically garbage collected.&lt;/p&gt;

&lt;p&gt;This algorithm considers zero referencing object as an object that is no longer used by the application.&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;var&lt;/span&gt; &lt;span class="nx"&gt;object1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;object2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="nx"&gt;object1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// object2 now has zero reference count, hence memory will be released&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;However, this algorithm has limitations with cycles.&lt;/p&gt;

&lt;p&gt;consider the below function,&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;cycleReference&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;object1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;object2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="nx"&gt;object1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;object2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// object1 references object2&lt;/span&gt;
    &lt;span class="nx"&gt;object2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;object1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// object2 references object1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;done&lt;/span&gt;&lt;span class="dl"&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;In the above function in which &lt;em&gt;object1&lt;/em&gt; is referenced to &lt;em&gt;object2&lt;/em&gt; and &lt;em&gt;object2&lt;/em&gt; is referenced to &lt;em&gt;object1&lt;/em&gt; and it creates a &lt;strong&gt;cycle&lt;/strong&gt;. &lt;br&gt;
When the scope goes out of the function, then these two objects are useless. However, the garbage collector is unable to free the memory since those two still got the reference to each other. &lt;br&gt;
It leads to memory leaks in the application.&lt;/p&gt;

&lt;p&gt;2). &lt;strong&gt;Mark and Sweep algorithm&lt;/strong&gt;.&lt;br&gt;
The garbage collector uses this algorithm to free memory when an object is unreachable, rather than a zero referencing object.&lt;/p&gt;

&lt;p&gt;The garbage collector will first find all the global objects or root objects and will find all the references to these global objects and references to the reference object, and so on. Using this algorithm, the garbage collector will identify the reachable and unreachable objects.&lt;br&gt;
All the unreachable objects will be automatically garbage collected.&lt;/p&gt;

&lt;p&gt;Let us try to implement something similar to this algorithm.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
