<?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>Forem: Vani Shivanand</title>
    <description>The latest articles on Forem by Vani Shivanand (@svaani).</description>
    <link>https://forem.com/svaani</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%2F310974%2F024470c5-136d-487c-a2f5-df38b184bb0e.jpg</url>
      <title>Forem: Vani Shivanand</title>
      <link>https://forem.com/svaani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/svaani"/>
    <language>en</language>
    <item>
      <title>Assigning [ ] performs better than Array(n) - Reports attached.</title>
      <dc:creator>Vani Shivanand</dc:creator>
      <pubDate>Wed, 15 Jul 2020 16:33:24 +0000</pubDate>
      <link>https://forem.com/svaani/performs-better-than-array-n-reports-attached-f0b</link>
      <guid>https://forem.com/svaani/performs-better-than-array-n-reports-attached-f0b</guid>
      <description>&lt;p&gt;Here is a small snippet that was measured using jsperf sit&lt;/p&gt;

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

&lt;p&gt;That means &lt;code&gt;Array(n)&lt;/code&gt; is a lot slower than &lt;code&gt;[]&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  What might be the reason?
&lt;/h2&gt;

&lt;p&gt;In the background there are different types of arrays though for a developer it looks like there is only one Array type.&lt;/p&gt;

&lt;p&gt;For the sake of scope, two types can be discussed here. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Holey element type&lt;/li&gt;
&lt;li&gt;Packed element type&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we initialize the array with a size, it create an array with Holey element type. Otherwise Packed element type. There are different cases when the javascript engine converts packed element array type into holey. &lt;/p&gt;

&lt;p&gt;One of it is a &lt;code&gt;delete&lt;/code&gt; operation. Another case is,&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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 snippet, though in the first line it creates a packed array. Since there are no elements on index 0,1,2 &amp;amp; 3, the javascript engine converts it into holey array.&lt;/p&gt;

&lt;h2&gt;
  
  
  That was background. What might be the reason for performance difference?
&lt;/h2&gt;

&lt;p&gt;For a packed array, all that the javascript engine needs to do to find an element is,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To check if the index is in the range &lt;em&gt;(zero to arr.length)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;return value if hasOwnProperty of that index is true &lt;em&gt;(as array is stored in exact object format with index as key)&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  What if it is a holey array?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;To check if the index is in the range &lt;/li&gt;
&lt;li&gt;hasOwnProperty of that index &lt;/li&gt;
&lt;li&gt;hasOwnProperty of Array.prototype&lt;/li&gt;
&lt;li&gt;hasOwnProperty of Object.prototype&lt;/li&gt;
&lt;li&gt;and the same with any/all extended entities&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because in a holey array, the javascript engine can't decide just based on the range &lt;em&gt;(zero to arr.length)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That's the reason, any operation on holey array takes several more steps than packed array causing performance difference. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference : &lt;a href="https://www.youtube.com/watch?time_continue=445&amp;amp;v=m9cTaYI95Zc&amp;amp;feature=emb_title"&gt;https://www.youtube.com/watch?time_continue=445&amp;amp;v=m9cTaYI95Zc&amp;amp;feature=emb_title&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>performance</category>
    </item>
    <item>
      <title>Why did I rewrite Momentjs format method using RegExp?</title>
      <dc:creator>Vani Shivanand</dc:creator>
      <pubDate>Sat, 11 Jul 2020 12:24:51 +0000</pubDate>
      <link>https://forem.com/svaani/momentjs-format-in-10-lines-using-regex-1035</link>
      <guid>https://forem.com/svaani/momentjs-format-in-10-lines-using-regex-1035</guid>
      <description>&lt;p&gt;The popular library momentjs is widely used to any date related calculations. For 10 lines of utility code, we end up using 18kb or 72.5kb &lt;code&gt;gz&lt;/code&gt; format minified momentjs file. It's not just download time, it also about the memory that it consumes to operate.&lt;/p&gt;

&lt;p&gt;Here is an effort to replace that momentjs format code with the RegExp functions with a very few lines.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you wish to learn about regEx, visit &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Format date in RegExp
&lt;/h3&gt;



&lt;div class="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;months&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;feb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apr&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;may&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jun&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jul&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aug&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sep&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;oct&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nov&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dec&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;//listing all the possible keys to regular exp&lt;/span&gt;
&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;regExMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;MMM&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;RegExp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;months&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;|&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;dddd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\b\w&lt;/span&gt;&lt;span class="sr"&gt;+/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;dd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\d{2}&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;YYYY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\d{4}&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;hh&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\d{2}(?=&lt;/span&gt;&lt;span class="sr"&gt;:&lt;/span&gt;&lt;span class="se"&gt;\d{2}&lt;/span&gt;&lt;span class="sr"&gt;:&lt;/span&gt;&lt;span class="se"&gt;\d{2})&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;mm&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\d{2}(?=&lt;/span&gt;&lt;span class="sr"&gt;:&lt;/span&gt;&lt;span class="se"&gt;\d{2}\s)&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;ss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\d{2}(?=&lt;/span&gt;&lt;span class="sr"&gt;.&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;A-Z&lt;/span&gt;&lt;span class="se"&gt;]{3})&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;//creating regular exp to query the string&lt;/span&gt;
&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;regExKeys&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;RegExp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;+&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="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;regExMap&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;|&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//adding it to Date default object &amp;amp; having an optional date param&lt;/span&gt;
&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tinyDateFormat&lt;/span&gt; &lt;span class="o"&gt;=&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;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//validations&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;invalid string&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;date&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;date&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;invalid date&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;//extracting the matchable words &amp;amp; looping through&lt;/span&gt;
  &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;regExKeys&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;regEx&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;regExMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;regEx&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;newStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newStr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regEx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;regExMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;regEx&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt;
      &lt;span class="c1"&gt;//arr.push(this.toString().match(Date.regExMap[regEx]));&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;newStr&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;h3&gt;
  
  
  Usage
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// var date1 = new Date().tinyDateFormat("MMM dd YYYY, hh:mm:ss");&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;date&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;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;tinyDateFormat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am coding on MMM dd YYYY at hh hours &amp;amp; mm minutes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Why did I write this?
&lt;/h2&gt;

&lt;p&gt;The companies do interview the people for library/framework knowledge. But if the preference of interview changes to basics or core concepts, everyone would start to get more stability and strength in basics or core. This does helps in the application quality. &lt;/p&gt;

&lt;p&gt;For this momentjs format method, it takes the same amount of time to learn the syntax by library as it takes to write the utility using &lt;code&gt;RegExp&lt;/code&gt; and get the job done.&lt;/p&gt;

&lt;p&gt;The above example may not be covering all the use-cases of momentjs format function. It's not to copy &amp;amp; use it as a small library. But to learn it and to write it again. &lt;/p&gt;

&lt;p&gt;In other than date format cases also, try to take time and write your utility functions on your own. Anyway we do spend time on algorithms. The same time can be spent in writing such a utility functions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It is not considered as re-inventing the wheel but it can be marked as "efficient in writing the code using core concepts for the sake of efficient app".&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Heard of &lt;code&gt;tip of ice-berg&lt;/code&gt; problem? For a few lines from library, we end up using the entire bundle.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;To be efficient may be more beneficial than being knowledgeable. Thanks for reading!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>webperf</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Why don't end products match exact UX design?</title>
      <dc:creator>Vani Shivanand</dc:creator>
      <pubDate>Thu, 02 Jul 2020 13:33:11 +0000</pubDate>
      <link>https://forem.com/svaani/why-don-t-end-products-match-exact-ux-designs-48ch</link>
      <guid>https://forem.com/svaani/why-don-t-end-products-match-exact-ux-designs-48ch</guid>
      <description>&lt;p&gt;After being in UI development for a couple of years, recently I got to explore a designing tool. There by I got to know the reason end products don't match the exact UX. &lt;/p&gt;

&lt;p&gt;There is a process that UX designers follow. Starting with developing a component mock-up collections, defining definite set of colors/gradients, constraint alignments, resolutions and more. The UI developers also have to follow the same procedure from developing UI component library(or customizing the existing library according to design), create color constants and respectively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Second, we should be able to read a design&lt;/em&gt;&lt;/strong&gt; which means some of the css properties are not mapped with the same name in the design. For example, in figma - border is named as stroke with the properties "inside &amp;amp; outside" which we need to map as padding &amp;amp; margin. Though figma gives a css translations to these, some times they need to be re-verified and also we might not use the exact styles. For example, left, top properties are rarely used in development when we are using column layout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Third, careful observations.&lt;/em&gt;&lt;/strong&gt; Designers take a good amount of time in details. For example, just to see if the shadow settings are looking good. If we just copy the shadow color and ignore the opacity of the shadow, then it may give a totally different look.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Four, analysis of deviation from the CSS library.&lt;/em&gt;&lt;/strong&gt; Design tools are very handy as we all know. Some text might have totally different style applied(may be font), Or may be a random color button has pitched in between. In any such cases, we should discuss again with the designers. May be, it was placed by mistake or may be it was not thought in terms of development constraints. When we discuss with the designers, they have better suggestions or they may even alter the design. So, it is good to not ignore the deviations and to settle with a random alternative. It will be visible in the end product.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Fifth, to focus on view part separately from logic&lt;/em&gt;&lt;/strong&gt; The moment design is provided &amp;amp; there is a demand for estimation, we totally get into implementation details. Divide the estimations into view part(html/css/android look &amp;amp; feel) and logic part. If possible, develop it as two different phases. That convinces the entire team about the progress, also providing a good quality.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is all I got to share for today. Thanks for reading&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ux</category>
      <category>javascript</category>
      <category>css</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Webpack style-loader, what does it cost?</title>
      <dc:creator>Vani Shivanand</dc:creator>
      <pubDate>Wed, 01 Jul 2020 18:06:23 +0000</pubDate>
      <link>https://forem.com/svaani/webpack-style-loader-what-does-it-cost-3f3l</link>
      <guid>https://forem.com/svaani/webpack-style-loader-what-does-it-cost-3f3l</guid>
      <description>&lt;p&gt;If we are not carefully writing our webpack config file, then may be the css styles are getting loaded via css-loader and style-loader through any default framework configurations. &lt;/p&gt;

&lt;p&gt;To revisit, css-loader converts the css file into javascript string. And style-loader injects that string to html dom. The former operation causes no issue as it happens during &lt;em&gt;say&lt;/em&gt;, compilation time. But the latter happens during run time which is, every time user loads the page. &lt;/p&gt;

&lt;p&gt;This is the &lt;a href="https://github.com/svaani/style-loader-test"&gt;link&lt;/a&gt; of a very minimal webpack configuration code. Here, bootstrap is considered as it is a pretty large library and for the sake of measuring the numbers. All that displays is a DOM alert message with bootstrap class. When we observe the performance, it causes one layout operation per css file which is shown as below.&lt;/p&gt;

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

&lt;p&gt;Many times, we have one css file per module. That means, if we have 20 modules, we will have 20 additional layout operations. &lt;/p&gt;

&lt;p&gt;So, we must carefully bundle the css using any external webpack loader and load it separately. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webpack</category>
      <category>css</category>
      <category>javascript</category>
      <category>webperf</category>
    </item>
    <item>
      <title>Frontend developer? Pull  nodejs code into your plate!</title>
      <dc:creator>Vani Shivanand</dc:creator>
      <pubDate>Sun, 21 Jun 2020 16:44:14 +0000</pubDate>
      <link>https://forem.com/svaani/frontend-developer-grab-nodejs-code-into-your-plate-3lii</link>
      <guid>https://forem.com/svaani/frontend-developer-grab-nodejs-code-into-your-plate-3lii</guid>
      <description>&lt;p&gt;It is very common that nowadays a huge lot of products have a middleware written in node. Writing this node code second time in my career, I would like to say, "why it should belong to a front end developer!"&lt;/p&gt;

&lt;h3&gt;
  
  
  A simple reason...
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;In every interview (if none is scheduled, it may even after 5 years), a backend engineer who has written node is going to ask questions on javascript.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  So what?, one may say.
&lt;/h3&gt;

&lt;p&gt;We might have coded in UI for say 5 years, 10 year or even 15-20 years. In UI, we follow almost an exact same pattern of javascript code that interacts with 2 more languages html &amp;amp; css. Yes, we covered object oriented concepts, es6 and more.&lt;/p&gt;

&lt;h3&gt;
  
  
  But what did we not cover?
&lt;/h3&gt;

&lt;p&gt;Front end developers, mostly have not handled the asynchronous behavior(apart from browser engine puts a wait OR for a server call).&lt;/p&gt;

&lt;p&gt;Also, we never thought of "how javascript can handle multiple requests at a time without being a multithread". &lt;em&gt;&lt;strong&gt;We never had to know about event loop until recent years.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;What would a backend developer who has coded node think?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This guy/girl has done javascript for years but yet doesn't know certain concepts which I(backend developer) could learn in a span of say 2-6 months. Something must be missing.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Well, not just that..
&lt;/h3&gt;

&lt;p&gt;Learning node or coding in node, gives us a lot better understanding of javascript through which we can think in terms of patterns better. We can choose a front end framework wisely. Or even code without a framework but by choosing open source tools for template-ing, fast-dom, MVC skeleton and more..&lt;/p&gt;

&lt;p&gt;It is also profitable to the company as they are enhancing the knowledge of the right person/team through which their UI also improves.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;That was a short write. Thanks for reading! Happy coding :)&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
      <category>codequality</category>
    </item>
    <item>
      <title>PNG Sprites perform a lot better than SVG (Reports Attached)</title>
      <dc:creator>Vani Shivanand</dc:creator>
      <pubDate>Sun, 29 Mar 2020 09:45:52 +0000</pubDate>
      <link>https://forem.com/svaani/png-sprites-perform-a-lot-better-than-svg-reports-attached-5agl</link>
      <guid>https://forem.com/svaani/png-sprites-perform-a-lot-better-than-svg-reports-attached-5agl</guid>
      <description>&lt;p&gt;Back then we have seen the designers giving us image sprites and then the background positions being changed accordingly for each icon and on-hover. There needs no mention that SVGs have captured all the attention as soon they pitched in. &lt;/p&gt;

&lt;p&gt;Let's peek into the performance of these. A couple of svg icons were taken and then converted into svg sprite and png sprite. &lt;/p&gt;

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

&lt;p&gt;On svg page, change &lt;code&gt;fill:color&lt;/code&gt; for hover color and in png page, change &lt;code&gt;background-position-y&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Two actions were performed while performance was recorded. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Page Reload&lt;/li&gt;
&lt;li&gt;Hover out and in for the second icon(bulb) - 4 times&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Summary of SVG Performance
&lt;/h3&gt;

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

&lt;h3&gt;
  
  
  Summary of PNG Performance
&lt;/h3&gt;

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

&lt;h3&gt;
  
  
  What's happening?
&lt;/h3&gt;

&lt;p&gt;Apart from scripting time, PNG performance beats loading and rendering(layout). &lt;/p&gt;

&lt;h3&gt;
  
  
  Why is this?
&lt;/h3&gt;

&lt;p&gt;PNG sprite helped to not load multiple images per icon. Thus there is a better &lt;code&gt;loading&lt;/code&gt; time.&lt;/p&gt;

&lt;p&gt;An svg is multiple pieces of dom elements where as an image is one dom element. &lt;code&gt;Rendering&lt;/code&gt; time will be a lot better due to this single element.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Painting&lt;/code&gt; time remains almost the same as the user would see exact same result on hover.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can a PNG sprite serve equivalent to SVG?
&lt;/h3&gt;

&lt;p&gt;Yes, with a bit more work(at least in case of icons). But the same on-hover effects, responsiveness can be achieved through PNG sprite as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  One Thing To NOT Miss
&lt;/h3&gt;

&lt;p&gt;Many use &lt;code&gt;content:'\000'&lt;/code&gt; for icons. May not be a good way. The reason being, the entire font with many other characters is being downloaded along to achieve this. Always download a minimal font file. In the most optimized sites, one can observe that font files are the largest bundles. The font files should come under 2-3kb.&lt;/p&gt;

&lt;p&gt;Find the github link for the exact files that were used to create the performance reports.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJ70wriM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/svaani"&gt;
        svaani
      &lt;/a&gt; / &lt;a href="https://github.com/svaani/sprite_performance"&gt;
        sprite_performance
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Entire Screenshot of SVG Sprite performance
&lt;/h2&gt;

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

&lt;h2&gt;
  
  
  Entire Screenshot of PNG Sprite performance
&lt;/h2&gt;

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

&lt;p&gt;&lt;em&gt;Thanks for reading&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>ux</category>
      <category>css</category>
    </item>
    <item>
      <title>Should you ignore scripting time of virtual-dom?</title>
      <dc:creator>Vani Shivanand</dc:creator>
      <pubDate>Sat, 21 Mar 2020 19:03:43 +0000</pubDate>
      <link>https://forem.com/svaani/should-you-ignore-scripting-time-of-virtual-dom-83a</link>
      <guid>https://forem.com/svaani/should-you-ignore-scripting-time-of-virtual-dom-83a</guid>
      <description>&lt;p&gt;In one of my post's comment section I made a statement saying,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Scripting time will be more because it was considered by any virtual-dom framework that to re-draw the tree should be optimized more than the amount of javascript that is being run.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A straight forward opinion might be to optimize scripting time is as important as it is to optimize Layout or DOM rendering. But we might want to remember web-workers here.&lt;/p&gt;

&lt;p&gt;Workers provide multi-threading or a parallel process along with the main thread but they don't have access to DOM and other entities. As we speak about virtual-dom getting overloaded with higher scripting time, the entire operations can be moved there.&lt;/p&gt;

&lt;p&gt;That's the reason, why the upcoming new opinion &lt;code&gt;virtual-dom is not needed&lt;/code&gt; might not survive longer. Frameworks/libraries with virtual dom should adapt to workers.&lt;/p&gt;

&lt;p&gt;Another issue with workers might be &lt;code&gt;Structured Cloning&lt;/code&gt; or deep-copying which needs to be worked upon. Because to communicate between them, a huge object needs to be passed. This can be fixed by taking an input from the developer on which part of the tree might be under the scope of change. Thus reducing the tree size that is being communicated between Worker and main thread.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>vue</category>
    </item>
    <item>
      <title>Negotiating For UI Optimization</title>
      <dc:creator>Vani Shivanand</dc:creator>
      <pubDate>Fri, 14 Feb 2020 11:46:21 +0000</pubDate>
      <link>https://forem.com/svaani/negotiating-for-ui-optimization-34fl</link>
      <guid>https://forem.com/svaani/negotiating-for-ui-optimization-34fl</guid>
      <description>&lt;p&gt;Speaking about small teams/companies, we hit this stage where we might be discussing with a product manager or a back-end experienced team lead about targets on front end performance optimization. And it is not a week/month's task to improve UI performance. It is a consistent effort and it also needs a parallel work of re-structuring the existing code. It's not that straight forward to negotiate on this with the team.&lt;/p&gt;

&lt;p&gt;These are the ways to communicate in a quite successful way.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Explain that JavaScript engine runs only single thread&lt;/strong&gt; --
Most of the back-end team leads or managers don't think about it as they usually run multiple threads whenever needed in back-end servers. And in JavaScript the usage of Web-workers is not that common.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DOM engine and JavaScript engine don't run in parallel&lt;/strong&gt; -- unless specified. Though many frameworks might be handling this internally, there is a need to revisit and see wherever we can induce async tasks between these engines.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It's end-user device and performance&lt;/strong&gt; -- unlike back-end code where the number of servers can be increased for a drop in performance, UI code has this limitation to run in each and every device with the minimal resources available. So there is a huge more work in here.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Saying these, it doesn't mean there is small performance work that needs to be done in back-end. There should be a lot. But the front end performance optimization is a lot different than that of back-end. And to communicate the same holds a higher importance. &lt;/p&gt;

&lt;p&gt;Once the team understands this, we can get resources to work on it and there on it helps in breaking down into tasks, learning about the performance, creating reports, A-B testing and a lot more similar plans. Otherwise, the team as a whole will always have a complain on the performance glitch. The performance neither gets addressed nor be ignored. This communication is a step to get rid of that situation.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for your time in reading this. Humbled! Please say a word in comments if anything needs to be considered or corrected.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>css</category>
    </item>
    <item>
      <title>If one knows, "how to use a framework" - it doesn't mean one knows the framework</title>
      <dc:creator>Vani Shivanand</dc:creator>
      <pubDate>Mon, 03 Feb 2020 15:24:47 +0000</pubDate>
      <link>https://forem.com/svaani/if-one-knows-how-to-use-a-framework-it-doesn-t-mean-one-knows-the-framework-34ib</link>
      <guid>https://forem.com/svaani/if-one-knows-how-to-use-a-framework-it-doesn-t-mean-one-knows-the-framework-34ib</guid>
      <description>&lt;p&gt;In simple words this post is about, &lt;em&gt;From the excitement of learning a syntax to diving deeper into the internals of frameworks and to make the right decision for each application.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;Yes, there are jobs out there that pay if one knows how to use a framework. They are good to start with. But in parallel, if the effort is not put in learning the basics of the language, the engine that runs and the interactivity - it may get hard to build a career by switching to learn the usage of frameworks.&lt;/p&gt;

&lt;p&gt;Let's take an instance of jquery vs core-javascript concepts. In my personal observation, jquery experts had more knowledge about jquery than a few javascript developers about javascript. And of course, javascript developers had to leave out a few job options. But in the long run, it is worth it as they get to learn any new framework with much ease and also they feel a lighter loss than a framework expert.&lt;/p&gt;

&lt;p&gt;If we take two-way binding or virtual-dom, we should put the effort into learning why they are needed and when. If we get to read, "use redux only when needed", it's good to take the next step in knowing why is it said so.&lt;/p&gt;

&lt;p&gt;If we don't do this, frameworks over frameworks will keep the developers rolling from one knowledge base to another.&lt;/p&gt;

&lt;p&gt;When enough developers do this, the companies can form a team of core-language developers and not use any frameworks in a lot many scenarios. Many companies take a decision to use a framework because it gives them stability due to the availability of framework-developers.&lt;/p&gt;

&lt;p&gt;In the long run, if we create framework developers, it would be an inefficient usage of &lt;code&gt;developer base&lt;/code&gt;' learning-time as someone who might have put 4-5 years in a framework might see another framework gaining more attention.&lt;/p&gt;

&lt;p&gt;This is not against any frameworks, they are needed because we lack in teams that can build the same with the base knowledge of a language alone. In the past, companies have seen instability with the same. Also, they are very much needed in a few scenarios where the requirements match the need.&lt;/p&gt;

&lt;p&gt;It was to remind us(especially myself) to learn any core language in depth.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I am a frontend developer. This may not apply for a few backend scenarios which I might not be aware of. Thanks for reading!&lt;/em&gt;   &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>jquery</category>
      <category>react</category>
    </item>
    <item>
      <title>:target Pseudo Selector And Performance</title>
      <dc:creator>Vani Shivanand</dc:creator>
      <pubDate>Thu, 30 Jan 2020 12:54:51 +0000</pubDate>
      <link>https://forem.com/svaani/target-pseudo-selector-and-performance-324d</link>
      <guid>https://forem.com/svaani/target-pseudo-selector-and-performance-324d</guid>
      <description>&lt;p&gt;To add an explanation in short on &lt;code&gt;:target&lt;/code&gt;, it applies the style to the element that has an id which is same as &lt;code&gt;location.hash&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As this post intends to discuss the usage, pros &amp;amp; cons of &lt;code&gt;:target&lt;/code&gt; selector, an example to explain the same in-depth is not considered. &lt;/p&gt;

&lt;p&gt;Say, there is a need for a slider or a tab, we usually apply the styles to all the slides or tabs. The slides or tabs that are not displayed, need not use the &lt;code&gt;Layout&lt;/code&gt; resources of the browser. There is absolutely no necessity to apply the style to the content that is not displayed to the user. &lt;/p&gt;

&lt;p&gt;If the particular slide/tab CONTENTS(not the tab title) can be given an id and set the hash, all the styles that need to be applied can be written inside &lt;code&gt;:target&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Yes, the additional styles have to be written to accommodate the changes that happen due to scroll to the element that happens after setting the hash.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Pros - A relevant usage
&lt;/h3&gt;

&lt;p&gt;It's good to consider this when the slide/tab is occupying most space on the screen because there's no scroll that will happen.&lt;/p&gt;

&lt;p&gt;Here is a github &lt;a href="https://github.com/svaani/target-pseudo-selector/blob/master/try2.html"&gt;link&lt;/a&gt; for a similar scenario that was tried. The only style that is applied to target in the example is to change the color. But as the relevant styles that are needed to an app are applied, this will be more beneficial. &lt;/p&gt;

&lt;h3&gt;
  
  
  Cons - Not a successful usage
&lt;/h3&gt;

&lt;p&gt;The same was tried to apply the style only to the content that is displayed on a scrollable content. Here is github &lt;a href="https://github.com/svaani/target-pseudo-selector/blob/master/try.html"&gt;link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a failed scenario as there will be a flickering that happens on the screen as the user scrolls. Speaking about performance, to set a new id on scroll should not be expensive as it is done not on every scroll event but in batches. Also, for the browser engine to erase the styles that were applied to the sections which the user has already passed, it shouldn't cause a lot of &lt;code&gt;Layout&lt;/code&gt; cost.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"block-custom"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;hello&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"block-custom"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;hello2&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"block-custom"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;hello3&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="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;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#focused_0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onscroll&lt;/span&gt; &lt;span class="o"&gt;=&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;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="c1"&gt;//- only chrome for now&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;range&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;caretRangeFromPoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&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="c1"&gt;// screen coordinates of upper-left corner of a scolled area (in this case screen itself) &lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;range&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;startContainer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentElement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// this an upper onscreen element&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;range&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="o"&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="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_&lt;/span&gt;&lt;span class="dl"&gt;"&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="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;focused_&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;//pushState can also be used&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#focused_&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;num&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;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.block-custom&lt;/span&gt;&lt;span class="nd"&gt;:target&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="no"&gt;red&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;If the flickering can be avoided, this can be a good benefit to the long scrolling content such as Facebook feed or similar.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading. Please help me with an update, if any alternatives are known without a flicker to apply style only to the content that is displayed on the screen(scrollable).&lt;/em&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Svelte VS ReactJS Performance Report</title>
      <dc:creator>Vani Shivanand</dc:creator>
      <pubDate>Sat, 11 Jan 2020 18:24:42 +0000</pubDate>
      <link>https://forem.com/svaani/svelte-vs-reactjs-performance-report-1hgj</link>
      <guid>https://forem.com/svaani/svelte-vs-reactjs-performance-report-1hgj</guid>
      <description>&lt;p&gt;A few days before a post was made on &lt;em&gt;Svelt Needs A Virtual DOM&lt;/em&gt;. Maybe there was a kind of lack of clarity in the same post from my side. To clarify it furthermore, the reports are attached here.&lt;/p&gt;

&lt;p&gt;Similar code that was run on both the frameworks was this,&lt;br&gt;
*&lt;em&gt;Not to mind for not using ES6 loops. This is due to switching from jsperf site&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;updateString&lt;/span&gt; &lt;span class="o"&gt;=&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;outerIncrement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;longStringPre&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;String number:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;outerIncrement&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;outerIncrement&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;componentDidMount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;updateString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;longString1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;longStringPre&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;longString2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;longStringPre&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;longString3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;longStringPre&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;longString4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;longStringPre&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt; &lt;span class="mi"&gt;1500&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;And the HTML as something like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div className="App2"&amp;gt;
        &amp;lt;div&amp;gt;hello&amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;&amp;lt;div&amp;gt;{this.state.longString1}&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;&amp;lt;div&amp;gt;&amp;lt;div&amp;gt;{this.state.longString2}&amp;lt;/div&amp;gt;{this.state.longString3}&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;&amp;lt;span&amp;gt;{this.state.longString4}&amp;lt;/span&amp;gt;&amp;lt;span&amp;gt;{this.state.longString2.split(":").join("--")}&amp;lt;/span&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;&amp;lt;div&amp;gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;{this.state.longString2.split(":").join("-")}&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;&amp;lt;div&amp;gt;{this.state.longString2.split(":").join("||")}&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;{this.state.longString1}&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;p&amp;gt;&amp;lt;span&amp;gt;{this.state.longString1.split(":").join("=")}&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That was reactjs code and the exact same code with the same timeouts &amp;amp; Html structure was followed with the svelte code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is the test code so complicated?
&lt;/h2&gt;

&lt;p&gt;Because an effort is made to replicate a scenario where multiple ajax calls responding at almost the same time. Most of our applications have a number of ajax calls which will end at the same time especially in a dashboard that contains multiple charts or when there are huge live-updates that happen.&lt;/p&gt;

&lt;h2&gt;
  
  
  To understand the test code
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Four strings &lt;code&gt;longString1, longString2,longString3 &amp;amp; longString4&lt;/code&gt; get updated at different time intervals. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the Html, for example, &lt;code&gt;longString2&lt;/code&gt; is being updated for every different position on the dom. This is to not allow both the frameworks to consider duplication. Because we usually don't have duplicated data when different ajax calls respond.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Nested DOM with multiple &lt;code&gt;div&lt;/code&gt; is because Reactjs alone compares the tree before updating the DOM. If I remove the nested DOM, Svelt performs better. But in our apps, we do have nested DOM.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are the consistent results from multiple attempts.&lt;br&gt;
Svelte screenshot1&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mCtnnZnP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/78wreaxd65ihpy9nsid0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mCtnnZnP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/78wreaxd65ihpy9nsid0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React screenshot1&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Pch5UW6C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mi33greq4inglkghpfdk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Pch5UW6C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mi33greq4inglkghpfdk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Svelte screenshot2&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BQeYDbW3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/wnmo8mn45i2uknrhd2zo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BQeYDbW3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/wnmo8mn45i2uknrhd2zo.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React screenshot2&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VpTYqlTj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/uoc2xhmdpbzva4nitjf1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VpTYqlTj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/uoc2xhmdpbzva4nitjf1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since we are speaking about Virtual DOM, we should consider DOM &amp;amp; Layout timings. We can observe that ReactJS has a good advantage over Svelte in the screenshots.&lt;/p&gt;

&lt;p&gt;And one thing to remember, DOM read is not expensive but DOM write is. Because on write the tree has to restructure itself. &lt;/p&gt;

&lt;p&gt;The entire story is only based on - anything that happens within a javascript engine has a lot less cost than communication with the DOM which is run by another engine(browser engine).&lt;/p&gt;

&lt;p&gt;Please feel free to comment to discuss the same. Thank you for your time.&lt;/p&gt;

</description>
      <category>svelte</category>
      <category>react</category>
      <category>vue</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Svelte Needs A Virtual DOM</title>
      <dc:creator>Vani Shivanand</dc:creator>
      <pubDate>Thu, 09 Jan 2020 11:49:27 +0000</pubDate>
      <link>https://forem.com/svaani/svelte-needs-a-virtual-dom-1ebm</link>
      <guid>https://forem.com/svaani/svelte-needs-a-virtual-dom-1ebm</guid>
      <description>&lt;p&gt;For decades, we were working without a Virtual DOM and it was accepted widely in the last decade soon after it was introduced mainly because the &lt;em&gt;benefits were seen&lt;/em&gt;. If we think that it is not needed, we need to do a lot more performance tests before we decide. Before the performance tests, here is an attempt to understand it through the basics.&lt;/p&gt;

&lt;p&gt;Let's consider the code,&lt;code&gt;&lt;br&gt;
let number = 1;&lt;br&gt;
number++;&lt;br&gt;
number=+2;&lt;br&gt;
number=+3;&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
*&lt;em&gt;This is only for the simplified illustration purpose. Please not to consider it as continued four updates. It intends to say four random DOM updates&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And HTML(or html alternative) being&lt;br&gt;
&lt;code&gt;&amp;lt;div&amp;gt;{number}&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The javascript engine &amp;amp; the browser engine communicate with each other four times as we are updating the &lt;code&gt;number&lt;/code&gt; four times. This is in the case of Angularjs &amp;amp; Svelte.&lt;/p&gt;

&lt;p&gt;But in the case of Reactjs or Vuejs or any framework with Virtual DOM, the javascript engine &amp;amp; the browser engine communicates only once(which means only one DOM update) and the Virtual DOM is altered four times.&lt;/p&gt;

&lt;p&gt;Coming to the user experience, Rich Harris in his video "Rethinking reactivity" shows a text input and demonstrates user typing experience in an input field. It looks great as there is no lag in Svelte. It's mostly perceived performance is spoken about but not real performance. Not exactly like chrome but it can be somewhere compared to chrome which uses most of the resources to perform better.&lt;/p&gt;

&lt;p&gt;In Angularjs &amp;amp; Svelte we need to optimize the above code again as below,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let displayNumber = number&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And HTML part as,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;div&amp;gt;{displayNumber}&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After this optimization, Angularjs &amp;amp; Svelte also have communication between two engines only once. Because every time the variable that's bound to the output is changed, both the engines need to communicate. When we change the bound variable only once as in &lt;code&gt;displayNumber&lt;/code&gt;, we have fixed the problem.&lt;/p&gt;

&lt;p&gt;Well, we can't have lint rules to watch this. So, this will be difficult to maintain without Virtual DOM.&lt;/p&gt;

&lt;p&gt;Back to the user experience as shown in Rich Harris video, while typing letters,  the engines communicate on every keystroke without Virtual DOM. But he is right that the user needs to feel that smooth feel of GUI update. The frustration meter metrics should be considered.&lt;/p&gt;

&lt;p&gt;The solution may be, the Virtual DOM frameworks should provide an optional entity to update a particular DOM element if anyone wishes to force-update the DOM directly for the chosen blocks. This way is a lot better than not using Virtual DOM at all.&lt;/p&gt;

&lt;p&gt;For the point "DOM is not slow", it is not just DOM but it is also about CSSOM which runs after every DOM update. In the case of Virtual DOM, CSSOM never runs unless the DOM is updated.&lt;/p&gt;

&lt;p&gt;In the end, it is nothing against Svelte or Angularjs. They are great frameworks with unique features. For lightweight products, one may use them but there is a need to re-think how the product is going to grow before we decide to choose.&lt;/p&gt;

&lt;p&gt;What I don't agree with him is when he said, "The only way to speed up your code is to get rid of it". There are obviously a lot many ways to speed up our code and that's why we are observing and working consistently. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o-YPj6r3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/dibzh2xoovkn4619kx4e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o-YPj6r3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/dibzh2xoovkn4619kx4e.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, please refer to the post that explains Virtual DOM with a very simple example &lt;a href="https://dev.to/svaani/is-virtual-dom-needed-1lo7"&gt;Virtual DOM - A Simplified Example&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And I plan to make the next post with performance screenshots with different code blocks on both the ways.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A comment on the above reading would be a lot helpful for me to improve my writings further. Thanks for the time!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>svelte</category>
      <category>vue</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
