<?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: Mert</title>
    <description>The latest articles on Forem by Mert (@mertsafak).</description>
    <link>https://forem.com/mertsafak</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%2F107840%2F0c8a0bec-8c35-41b2-9c10-ec9d8a23bd77.jpeg</url>
      <title>Forem: Mert</title>
      <link>https://forem.com/mertsafak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mertsafak"/>
    <language>en</language>
    <item>
      <title>Memoization in JavaScript</title>
      <dc:creator>Mert</dc:creator>
      <pubDate>Mon, 02 Mar 2020 20:26:39 +0000</pubDate>
      <link>https://forem.com/mertsafak/memoization-in-javascript-4ln1</link>
      <guid>https://forem.com/mertsafak/memoization-in-javascript-4ln1</guid>
      <description>&lt;h2&gt;What is memoization?&lt;/h2&gt;

&lt;p&gt;&lt;b&gt;Memoization&lt;/b&gt; is a technique that makes your function calls faster in exchange of memory space. Although it is generic for all programing languages, I'm going to use JavaScript to implement a simple memoization function.&lt;/p&gt;

&lt;h2&gt;Why to use it?&lt;/h2&gt;

&lt;p&gt;As our application grows, there may be some cases where hard computational algorithms might happen to be slowing down your application. You may want to store those computations' results and use them over and over again to improve your application's performance and that's where memoization comes in...&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Memoization&lt;/b&gt; technique gives you the ability of caching results in function scope and lets you use it from inner functions. This caching mechanism provides you better performance especially in terms of expensive function calls.&lt;/p&gt;

&lt;p&gt;Let's say you have a simple factorial function and you don't want to calculate factorial of any given number after it has already been calculated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx1j3dplgw3u34hu9ujxm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx1j3dplgw3u34hu9ujxm.png" alt="Factorial function image" width="800" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By returning a function and storing factorial result in its closure, we can create a memory cache. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdbdz0sw8gk21n5211jsd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdbdz0sw8gk21n5211jsd.png" alt="Memoize" width="800" height="1245"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As shown above, we created a memoizedFactorial function and store every calculated factorial in the cache object. This way we create closure for returning function and it has access to this cache object therefore second function call with same parameter will not call factorial function.&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;We prevent our application from calling function over and over again in the exchange of memory space because closured data is stored on heap memory.&lt;/p&gt;

&lt;p&gt;Also you can read;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/mertsafak/how-to-handle-dynamic-import-errors-in-vuejs-2d51"&gt;How to Handle Dynamic Import Errors in Vuejs&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webperf</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Handle Dynamic Import Errors in Vuejs</title>
      <dc:creator>Mert</dc:creator>
      <pubDate>Wed, 26 Feb 2020 11:50:19 +0000</pubDate>
      <link>https://forem.com/mertsafak/how-to-handle-dynamic-import-errors-in-vuejs-2d51</link>
      <guid>https://forem.com/mertsafak/how-to-handle-dynamic-import-errors-in-vuejs-2d51</guid>
      <description>&lt;p&gt;Nowadays, one of the most challenging parts of frontend development is &lt;b&gt;error handling&lt;/b&gt;. Sometimes we don't want to think about the worst cases or we just don't have time to handle them because of deadlines but it doesn't mean they aren't important.&lt;/p&gt;

&lt;p&gt;Today, I'm going to talk about &lt;b&gt;dynamic import error handling&lt;/b&gt;. Although this topic is about Vuejs, you can see this as a generic solution for other view libraries.&lt;/p&gt;

&lt;p&gt;As our application grows, we may want to reduce JavaScript bundle size in order to decrease initial load time and performance. There are couple of techniques to achieve this goal like reducing image sizes, lazy loading and code splitting.&lt;/p&gt;

&lt;p&gt;Code splitting is a technique which allows you to split your JavaScript bundles into different parts which can be loaded at anytime in the future. We can split route codes with JavaScript's &lt;b&gt;import()&lt;/b&gt; method but due to nature of it, it returns a promise after completion or failure of an asynchronous operation.&lt;/p&gt;

&lt;h2&gt;How&lt;/h2&gt;

&lt;p&gt;In Vuejs, we can set import methods return value to component key of route object like below. With this way JavaScript bundle of About page will be only called if user navigate to it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZEteYN9q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/9pzqap8h3dfwoyjrh72e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZEteYN9q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/9pzqap8h3dfwoyjrh72e.png" alt="Not handling error" width="800" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this code above, we only handle the success scenario. If we want to handle failure case, we can create a generic component which isn't imported dynamically and then return it on catch like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DssCs10w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/qkalbfdjs4j4giwnxlyq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DssCs10w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/qkalbfdjs4j4giwnxlyq.png" alt="Handling error" width="800" height="474"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This generic DynamicImportError component will be loaded on initial request. What if our initial request fails? Well... I think that is different problem.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;You can say that DynamicImportError will increase initial bundle size, but it is a dummy component and it only informs visitors that something went wrong so it won't have a big size. In my opinion it has a pretty low opportunity cost.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>webdev</category>
      <category>webpack</category>
    </item>
  </channel>
</rss>
