<?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: Muhammad Hasnain</title>
    <description>The latest articles on Forem by Muhammad Hasnain (@hasnaindev).</description>
    <link>https://forem.com/hasnaindev</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%2F436694%2F54ddcbfc-be5a-482e-a233-5862320225a6.jpg</url>
      <title>Forem: Muhammad Hasnain</title>
      <link>https://forem.com/hasnaindev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hasnaindev"/>
    <language>en</language>
    <item>
      <title>Design Pattern: Singleton - Probably the easiest explanation!</title>
      <dc:creator>Muhammad Hasnain</dc:creator>
      <pubDate>Mon, 16 Aug 2021 07:03:07 +0000</pubDate>
      <link>https://forem.com/hasnaindev/design-pattern-singleton-5682</link>
      <guid>https://forem.com/hasnaindev/design-pattern-singleton-5682</guid>
      <description>&lt;h3&gt;
  
  
  Table of Contents
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;What is Singleton?&lt;/li&gt;
&lt;li&gt;Implementation.&lt;/li&gt;
&lt;li&gt;When to Use?&lt;/li&gt;
&lt;li&gt;An Anti-pattern?&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  What is Singleton?
&lt;/h1&gt;

&lt;p&gt;Singleton is a design pattern that falls under the category of creational design patterns. Creational design patterns are associated with the simple creation of objects.&lt;/p&gt;

&lt;p&gt;Singleton itself ensures that at any given point during the software's runtime, there should be only one instance of an object. Usually, this object is available to the software, globally.&lt;/p&gt;

&lt;h1&gt;
  
  
  Implementation
&lt;/h1&gt;

&lt;p&gt;Before we talk about when to use it, let's quickly implement it and then we can try to understand the nitty-gritty details.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Database&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nv"&gt;$instance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Connect to database&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;get_instance&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;Database&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;is_null&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;self&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="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;instance&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;p&gt;If you look at the implementation, you see that the constructor is made private in order to prevent you from creating new instances of the class because database connections in a large system may be expensive.&lt;/p&gt;

&lt;p&gt;We use private and static variable &lt;code&gt;instance&lt;/code&gt; to store the instance of the Database class.&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;get_instance&lt;/code&gt; static method, we access the instance of the Database class, if in case the instance does not exist, we make sure that it does and then return the instance of the class.&lt;/p&gt;

&lt;h1&gt;
  
  
  When to Use?
&lt;/h1&gt;

&lt;p&gt;Use the singleton pattern only when you need an object to have a single instance throughout your software's runtime. From the example above, you can see that a connection to database is usually required only once.&lt;/p&gt;

&lt;p&gt;Any other such expensive operations or global configurations should use singleton but beware that singleton is also called, "An Anti-pattern." It should be almost always be avoided.&lt;/p&gt;

&lt;h1&gt;
  
  
  An Anti-pattern?
&lt;/h1&gt;

&lt;p&gt;Singleton is one of the patterns that is easy to understand and use. Thus, it may encourage engineers (at least new ones) to overuse it. It should be used only if there is no alternative solution.&lt;/p&gt;

&lt;p&gt;Singleton also encourages the use of global variables. For any engineer to keep track of global variables, it increases cognitive load for them. Such cognitive load may lead to poor software. More on cognitive load and psychology later.&lt;/p&gt;

&lt;p&gt;Besides, singleton is more or less, a unique instance of a global variable. It can introduce unnecessary restrictions, make unit tests a pain since they may rely on inheritance among other things.&lt;/p&gt;

&lt;p&gt;Some call singleton pattern an "evil pattern." Design patterns in general are agreed upon solutions to commonly recurring problems. Systems may use it when there is a good reason to do so, thus knowing this pattern is essential.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>php</category>
      <category>architecture</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Four Ways to Clone Array with Ease! Easiest Method to Clone Array.</title>
      <dc:creator>Muhammad Hasnain</dc:creator>
      <pubDate>Fri, 30 Jul 2021 09:16:51 +0000</pubDate>
      <link>https://forem.com/hasnaindev/four-ways-to-clone-array-with-ease-easiest-method-to-clone-array-3ipn</link>
      <guid>https://forem.com/hasnaindev/four-ways-to-clone-array-with-ease-easiest-method-to-clone-array-3ipn</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Visit my personal blog for more awesome content. &lt;a href="https://hasnain.dev/" rel="noopener noreferrer"&gt;hasnain.dev&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unlike primitive types, arrays cannot be cloned by simply assigning them to another variable, doing that only stores reference in another variable. Fear not! There are plenty of ways in which you can make a new copy of an array using four methods!&lt;/p&gt;

&lt;h2&gt;
  
  
  Array.concat
&lt;/h2&gt;



&lt;div class="highlight js-code-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;fruits&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;🍎&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;🍌&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;🍐&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cloned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Array.from
&lt;/h2&gt;



&lt;div class="highlight js-code-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;fruits&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;🍎&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;🍌&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;🍐&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cloned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Array.slice
&lt;/h2&gt;



&lt;div class="highlight js-code-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;fruits&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;🍎&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;🍌&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;🍐&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cloned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Spread Operator
&lt;/h2&gt;



&lt;div class="highlight js-code-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;fruits&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;🍎&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;🍌&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;🍐&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cloned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Practice
&lt;/h2&gt;

&lt;p&gt;Some of these aren't best practice. It really depends on what you're trying to achieve. For instance, Array.from converts iterables into an array. Array.concat concatenate two arrays. Array.slice gives you a part of an array and the spread operator simply spreads an array into argument list.&lt;/p&gt;

&lt;p&gt;Best practice depends on what purpose you are using these methods for.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>programming</category>
      <category>node</category>
    </item>
    <item>
      <title>Dynamic Module Pattern for JavaScript! Dynamically load JavaScript bundles.</title>
      <dc:creator>Muhammad Hasnain</dc:creator>
      <pubDate>Fri, 30 Jul 2021 07:21:13 +0000</pubDate>
      <link>https://forem.com/hasnaindev/dynamic-module-pattern-for-javascript-dynamically-load-javascript-bundles-m5c</link>
      <guid>https://forem.com/hasnaindev/dynamic-module-pattern-for-javascript-dynamically-load-javascript-bundles-m5c</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Original post on my personal blog. &lt;a href="https://hasnain.dev/" rel="noopener noreferrer"&gt;hasnain.dev&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Please make sure that you are already aware of the code-splitting and dynamic import mechanism. This pattern is module bundler agonistic as long as it provides code splitting and dynamic import features. With some work, you can have own implementation of this pattern.&lt;/p&gt;

&lt;p&gt;Users often leave a website in about 10 to 20 seconds as soon as they visit it. Slow websites increases bounce rate. If a website is slow and have a high bounce rate, it will inevitably drop the website ranking. Besides, even if it maintains good score, users will have no choice but to leave the website after a few seconds.&lt;/p&gt;

&lt;p&gt;If performance of a website is an utmost priority, we may implement lazy loading of images, use picture elements, use caching, CDN, among tons of other optimization techniques. However, the problem that I noticed was that people were struggling with loading JavaScript on demand.&lt;/p&gt;

&lt;p&gt;Hand-picking JavaScript libraries has its own problems. For instance, we might need to include different script tags for each page that makes use of certain markup or library. This can lead to clutter and maintenance issues. Some may have an arbitrary implementations that may or may not work under certain conditions. Even Google Tag Manager can be cumbersome.&lt;/p&gt;

&lt;p&gt;In order to solve this problem, I introduce...&lt;/p&gt;

&lt;h1&gt;
  
  
  The Dynamic Module Pattern
&lt;/h1&gt;

&lt;p&gt;The Dynamic Module Pattern is a pattern where you define, right in your markup, what associated JavaScript modules should be loaded. Suppose, you have a slider module in your app that makes use of the library called, &lt;a href="https://flickity.metafizzy.co/" rel="noopener noreferrer"&gt;flickity.js&lt;/a&gt;. When you include the markup, the &lt;em&gt;dynamic module pattern&lt;/em&gt; will load appropriate JavaScript bundles for it and if you completely remove the slider, no JavaScript will be loaded. You don't have to worry about manually removing it.&lt;/p&gt;

&lt;p&gt;This not only takes away the headache of micromanaging libraries in your markup using script tags or a list of &lt;em&gt;if&lt;/em&gt; statements in case you are using a templating engine. Another great thing about this pattern is that you don't really need to worry about where the markup is coming from as long as certain attributes are defined (See explanation section on more benefits).&lt;/p&gt;

&lt;p&gt;For instance, it could be a Shopify &lt;em&gt;snippet&lt;/em&gt; or &lt;em&gt;section&lt;/em&gt;. A WordPress &lt;em&gt;post&lt;/em&gt; or &lt;em&gt;shortcode&lt;/em&gt;, Laravel or Node based server-side sites using templates, static sites, this pattern also work perfectly with all of them. Unless of course, your development environment already provides a code-splitting mechanism like &lt;em&gt;create-react-app&lt;/em&gt; or &lt;em&gt;vue-cli&lt;/em&gt;, in which case you don't really have to worry about this.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does it Work?
&lt;/h2&gt;

&lt;p&gt;I'll provide the code snippets and after that I'll explain what's going on. I'm using this pattern for a WordPress theme that uses WebPack and Svelte. The same can be done for React or Vue especially if you are making isolated snippets or widgets. The Shortcode provides user the ability to give it a module name and the associated JavaScript bundle will be loaded for it. Magic! 🎩&lt;/p&gt;

&lt;h3&gt;
  
  
  Markup
&lt;/h3&gt;



&lt;div class="highlight js-code-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;data-module=&lt;/span&gt;&lt;span class="s"&gt;"slider"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  JavaScript
&lt;/h3&gt;



&lt;div class="highlight js-code-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;modules&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&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="nf"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-module]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;module&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;const&lt;/span&gt; &lt;span class="nx"&gt;componentName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;data-module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`./components/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;componentName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.svelte`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;component&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="nx"&gt;component&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
          &lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;componentName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.svelte loaded.`&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;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;componentName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.svelte failed to load.`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&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;h3&gt;
  
  
  Explanation
&lt;/h3&gt;

&lt;p&gt;The HTML is quite simple. We define a simple HTML &lt;em&gt;div&lt;/em&gt; element with the attribute of &lt;em&gt;data-module&lt;/em&gt; which is also the name of the &lt;em&gt;component&lt;/em&gt; aka &lt;em&gt;file&lt;/em&gt; that we need to import in order to bring this component to life. This element is simply the &lt;em&gt;&lt;a href="https://svelte.dev/docs#Client-side_component_API" rel="noopener noreferrer"&gt;root&lt;/a&gt;&lt;/em&gt; element for svelte slider component.&lt;/p&gt;

&lt;p&gt;The JavaScript is however, interesting. It first fetches all the elements present in DOM that have the &lt;em&gt;data-module&lt;/em&gt; attribute defined. It loops through all those elements and for each single one, it gets the &lt;em&gt;data-module&lt;/em&gt; attribute.&lt;/p&gt;

&lt;p&gt;After that, it tries to dynamically import a certain &lt;em&gt;component&lt;/em&gt; that exists in the components folder (&lt;strong&gt;./components/{component-name}.extension&lt;/strong&gt;). If the component is successfully loaded, we're notified instantly. If the component is not present or fails to load, we receive a warning.&lt;/p&gt;

&lt;p&gt;The best thing about this pattern is that I can add and remove this markup or I can use it multiple times in my page. This pattern will make sure that the appropriate JavaScript is loaded or not loaded.&lt;/p&gt;

&lt;p&gt;Does this load JavaScript bundles multiple times if I make use of the &lt;em&gt;data-module&lt;/em&gt; several times in the page markup? Please, keep reading. I'll answer that soon!&lt;/p&gt;

&lt;h2&gt;
  
  
  Without Module Bundlers?
&lt;/h2&gt;

&lt;p&gt;You can definitely modify this pattern to fit your needs. For instance, you can use intersection observers and or events like key events, mouse events, hover, scroll and what not to dynamically load JavaScript. Imagine, you can prefetch or pre-connect components on user events and fetch them whenever they are needed. 🚀&lt;/p&gt;

&lt;p&gt;Like I said, you can use this pattern without module bundlers. You can implement the &lt;em&gt;Dynamic Module Pattern&lt;/em&gt; using a custom import statement that can load JavaScript from CDN or locally from your own website. However, keep in mind that this may not be as easy as you think. There are several problems that you need to keep in mind.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caveats With Custom Implementations
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Repeated &lt;em&gt;data-module&lt;/em&gt; Elements:&lt;/strong&gt; If an element repeated more than once, a naïve implementation will dynamically load script tags for each individual element. For instance, if an element is used in four places that makes use of bundles that weight about 80 KBs, you've just downloaded 320 KBs of JavaScript!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dependencies:&lt;/strong&gt; This is a major issue with custom implementations. Module bundlers can easily nest or map out the dependency tree but in custom implementation, any bundle that is imported must be and need to be available at a global scope, unless they are isolated containers that one does not really need to worry about.&lt;/p&gt;

&lt;p&gt;This also begs the question of, "What if I need to load &lt;a href="https://flickity.metafizzy.co/" rel="noopener noreferrer"&gt;flickity.js&lt;/a&gt; and then my custom JavaScript in order to make my slider functional?" This is an actual issue. You'd have to handle the dependency tree on your own which is not a simple task IMO.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caveat With Original Implementations
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Parallel Scripts Loading:&lt;/strong&gt; This pattern can be definitely tweaked in order to support parallel script loading. Right now, my method does not support that. For instance, you can load &lt;em&gt;Vue&lt;/em&gt; along with your custom JavaScript bundle for which &lt;em&gt;Vue&lt;/em&gt; is a dependency. As soon as they are both loaded, you appropriately initialize them by passing &lt;em&gt;Vue&lt;/em&gt; as a parameter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Naïve Custom Implementation
&lt;/h2&gt;

&lt;p&gt;This is just for fun in case you want to test things about without the headache of setting up a module bundler!&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;const&lt;/span&gt; &lt;span class="nx"&gt;customImport&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;const&lt;/span&gt; &lt;span class="nx"&gt;script&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="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;script&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onerror&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reject&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="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;modules&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&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="nf"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-module]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;module&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;const&lt;/span&gt; &lt;span class="nx"&gt;componentName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;data-module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// This could be anything, CDN or a local asset.&lt;/span&gt;
  &lt;span class="nf"&gt;customImport&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;componentName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.extension`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&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="c1"&gt;// script context IE window.Vue etc&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;failure&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&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;Let me know if you found &lt;em&gt;Dynamic Module Pattern&lt;/em&gt; helpful. Please, do share your thoughts on this, I would love to hear how this might help you. Please, make sure to react and share this too. Thank you for reading!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover image credits: **Anthony Shkraba&lt;/em&gt;* from &lt;strong&gt;Pexels&lt;/strong&gt;*&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webpack</category>
      <category>webdev</category>
      <category>wordpress</category>
    </item>
    <item>
      <title>What is your go-to state management library? No frameworks!</title>
      <dc:creator>Muhammad Hasnain</dc:creator>
      <pubDate>Tue, 13 Jul 2021 22:25:36 +0000</pubDate>
      <link>https://forem.com/hasnaindev/what-is-your-go-to-state-management-library-no-frameworks-53jb</link>
      <guid>https://forem.com/hasnaindev/what-is-your-go-to-state-management-library-no-frameworks-53jb</guid>
      <description>&lt;p&gt;Hello.&lt;/p&gt;

&lt;p&gt;What is your go-to state management library? I know we can use redux without React and I have. Redux is such an amazing tool. But, are there any alternatives? PS, if possible, also share how the library you mentioned saved your bum.&lt;/p&gt;

&lt;p&gt;I remember I was working on a project where I was not going to use any JavaScript frameworks. Somewhat complex google maps system for a WordPress multisite. For each site we used to input the associated shop/branch coordinates and then polygon information for coverage area.&lt;/p&gt;

&lt;p&gt;Since, there were over 25 sites, I cached all the settings in a new table that I had created in order to fetch all the data in a single query instead of changing the "blog", fetching that information for that specific site and changing it again.&lt;/p&gt;

&lt;p&gt;The data was complex but I handled it beautifully. After I was done, I thought only if I had used Redux in order to make state management super super easy! My project did use custom observers, immutable data and selectors. Replacing that with Redux would've meant that other developers would've started with it like real easy!&lt;/p&gt;

&lt;p&gt;Anyways, thanks for reading this post. Give me some awesome recommendation and also, how the library you mentioned saved you bum! Thanks!&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to lazy-load images? The easiest way to lazy-load images on your website! 🖼</title>
      <dc:creator>Muhammad Hasnain</dc:creator>
      <pubDate>Sat, 05 Jun 2021 00:38:39 +0000</pubDate>
      <link>https://forem.com/hasnaindev/how-to-lazy-load-images-the-easiest-way-to-lazy-load-images-on-your-website-249</link>
      <guid>https://forem.com/hasnaindev/how-to-lazy-load-images-the-easiest-way-to-lazy-load-images-on-your-website-249</guid>
      <description>&lt;p&gt;Lazy-loading is the simple process of loading resources only when they are needed. These resources can be anything, from images to stylesheets, fonts, scripts, iframes etc.&lt;/p&gt;

&lt;p&gt;This helps improve the performance of your website drastically because client does not have to download the images, videos and other resources when they aren't required. 🥳🎈&lt;/p&gt;

&lt;p&gt;We're going to use the package called, "&lt;a href="https://www.npmjs.com/package/lozad" rel="noopener noreferrer"&gt;Lozad.&lt;/a&gt;" To lazy-load images. &lt;a href="https://www.npmjs.com/package/lozad" rel="noopener noreferrer"&gt;Lozad.&lt;/a&gt; will only add 1 KB to your production 📦! Check out &lt;a href="https://bundlephobia.com/result?p=lozad@1.16.0" rel="noopener noreferrer"&gt;Bundlephobia &lt;/a&gt; 😱 for more info.&lt;/p&gt;

&lt;p&gt;We're only going to lazy-load images but &lt;a href="https://www.npmjs.com/package/lozad" rel="noopener noreferrer"&gt;Lozad&lt;/a&gt; can handle lazy-loading for srcsets, background images, videos and iframes too. So, let's include the package and start hacking! 🪓&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i lozad
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Include &lt;a href="https://www.npmjs.com/package/lozad" rel="noopener noreferrer"&gt;Lozad&lt;/a&gt; in your entry or main JavaScript file. Alternatively, you could use &lt;a href="https://cdn.jsdelivr.net/npm/lozad/dist/lozad.min.js" rel="noopener noreferrer"&gt;Lozad CDN&lt;/a&gt; if you don't have module bundler set up.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;lozad&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lozad&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Makes sure to run the library when DOM loads.&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DOMContentLoaded&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;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="cm"&gt;/**
   * This is all you need to do!
   * Check the link for advanced usages.
   * https://www.npmjs.com/package/lozad
   */&lt;/span&gt;
   &lt;span class="nf"&gt;lozad&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;observe&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;By default, &lt;a href="https://www.npmjs.com/package/lozad" rel="noopener noreferrer"&gt;Lozad&lt;/a&gt; will look for elements with class, "lozad". When the element is about to enter the viewport, &lt;a href="https://www.npmjs.com/package/lozad" rel="noopener noreferrer"&gt;Lozad&lt;/a&gt; takes the &lt;em&gt;data-src&lt;/em&gt; or other such &lt;a href="https://www.npmjs.com/package/lozad" rel="noopener noreferrer"&gt;Lozad&lt;/a&gt; related attributes and assigns it to the &lt;em&gt;src&lt;/em&gt; attribute to load the image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"lozad"&lt;/span&gt; &lt;span class="na"&gt;data-src=&lt;/span&gt;&lt;span class="s"&gt;"https://example.com/img.jpg"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it, you've successfully added support for lazy-loading 🎉🧨🎊!&lt;/p&gt;

&lt;p&gt;Best practice would be to leave the images that are in the header and lazy-load only the ones that are below the initial viewport.&lt;/p&gt;

&lt;h4&gt;
  
  
  Challenge!
&lt;/h4&gt;

&lt;p&gt;Use Lozad to lazy-load a background image and video!&lt;/p&gt;

&lt;h4&gt;
  
  
  Thoughts?
&lt;/h4&gt;

&lt;p&gt;Please, share your thoughts and experiences as to how lazy-loading improved your website. PS, are you guys interested in me writing about lazy-loading using vanilla JavaScript? Let me know in the comments, thank you!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>wordpress</category>
      <category>optimization</category>
    </item>
    <item>
      <title>NodeJS Worker Thread on EC2 Instance with single thread?</title>
      <dc:creator>Muhammad Hasnain</dc:creator>
      <pubDate>Fri, 21 May 2021 17:44:06 +0000</pubDate>
      <link>https://forem.com/hasnaindev/nodejs-worker-thread-on-ec2-instance-with-single-thread-19m6</link>
      <guid>https://forem.com/hasnaindev/nodejs-worker-thread-on-ec2-instance-with-single-thread-19m6</guid>
      <description>&lt;p&gt;Hi!&lt;/p&gt;

&lt;p&gt;I'm not a 100% familiar with NodeJS workers and clusters. I know that we can make use of multiple threads which is amazing to say the least.&lt;/p&gt;

&lt;p&gt;I'm working on a small personal project that I've hosted on an free EC2 instance and I see that there is one CPU and one thread available. Is there then any benefit of using worker threads?&lt;/p&gt;

&lt;p&gt;Also, why does the &lt;a href="https://nodejs.org/api/cluster.html" rel="noopener noreferrer"&gt;NodeJS Cluster documentation&lt;/a&gt; fork a cluster for a single CPU instead of a thread?&lt;/p&gt;

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

</description>
      <category>node</category>
      <category>devops</category>
      <category>aws</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How would you tackle this NodeJS project?</title>
      <dc:creator>Muhammad Hasnain</dc:creator>
      <pubDate>Wed, 19 May 2021 15:11:26 +0000</pubDate>
      <link>https://forem.com/hasnaindev/how-would-you-tackle-this-nodejs-project-43if</link>
      <guid>https://forem.com/hasnaindev/how-would-you-tackle-this-nodejs-project-43if</guid>
      <description>&lt;p&gt;Hello!&lt;/p&gt;

&lt;p&gt;I work at a startup and have been working on a cool project NodeJS based project. Even though, it is a WordPress position, when I'm free, I take my time to work on interesting projects. Given what we're trying to achieve in this soon-to-be proprietary system, I noticed optimization issues with it. I tried bombarding the endpoints with 200 requests at a time and &lt;code&gt;pm2 monit&lt;/code&gt; showed a few issues.&lt;/p&gt;

&lt;p&gt;Before I talk about the pm2 stats, I would like to talk about what the system actually does. We sent a domain name to our endpoint, for example, say, dev.to. The endpoint makes an entry to database and it emits an event to analyze that website. The endpoint quickly does what it is supposed and sends back a 200 response but the process afterwards takes a lot of time as it involves HTTP requests a huge sets of things it needs to analysis the website for, a long with Puppeteer, potentially a lot of loops with hundreds, if not thousands of iterations.&lt;/p&gt;

&lt;p&gt;What I talked about above, resulted in at almost 100% of CPU usage in PM2 stats, the heap was almost 100% too, EventEmitter gave us memory leaks warnings. Given that there is no queue or in-memory DS like Redis, I think the event loop was overwhelmed with 200 requests at the same time which involves a lot of processing! I'm not happy with the results and will present issues further down the road.&lt;/p&gt;

&lt;p&gt;NOTE: Keep in mind that the slow process I am talking about takes place on an app level. The ExpressJS route only emits and event to start the process for that specific domain that it just saved into the database. I did this because there was no need to keep a user waiting for 10 seconds!&lt;/p&gt;

&lt;p&gt;I discussed this with my boss and he encouraged me to take time and ask for help around the community. This is why I am here! Have you worked on such a project? If so, how did you handle it? What would you recommend me to do in this case? Should I go for cronjobs instead of event based system? Would really appreciate an answer.&lt;/p&gt;

&lt;p&gt;Thank you! Also, if you have any questions regarding the project, let me know and I shall answer them.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>npm</category>
      <category>nginx</category>
      <category>devops</category>
    </item>
    <item>
      <title>Introducing Browsermeta: Debugging made (super) easy!</title>
      <dc:creator>Muhammad Hasnain</dc:creator>
      <pubDate>Mon, 22 Mar 2021 19:14:36 +0000</pubDate>
      <link>https://forem.com/hasnaindev/introducing-browsermeta-debugging-made-super-easy-1lo5</link>
      <guid>https://forem.com/hasnaindev/introducing-browsermeta-debugging-made-super-easy-1lo5</guid>
      <description>&lt;p&gt;Sometimes you write code that runs exactly as you expected but fails to run for a client on their machine. Even though your code may pass all tests and runs correctly for you on your system and machine, it may still fail because the non tech savvy client may be using an outdated browser, a different browser vendor, machine with drastically different resolutions, extensions, lives in a different time zone.&lt;/p&gt;

&lt;p&gt;Such issues are hard to debug because you might not be expecting them. It can be very cumbersome to ask client about their machine and browser especially if the client is a non tech savvy and or a busy person. There are many tools that monitors your code, send logs to the cloud and generate reports but non tools that gives you information about the client's machine and browser.&lt;/p&gt;

&lt;p&gt;As a startup, we've faced such issues. Once, we created some popups that were not being shown to the client apparently AdBlock extension was blocking our popup even though the popup did not contain any advertisement. Another such issue occurred due to client's time zone because of which the time based filter that we created failed to filter data properly, we then opted to using moment.js to solve that problem.&lt;/p&gt;

&lt;p&gt;Such issues will always occur and can be very easily solved if we have more information and understanding of what system the client is relying or and using. That is why we created Browsermeta. Browsermeta is a completely free tool that collects information about the client's machine and browser, which is uploaded to the cloud and gives them a link which they can share with you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://chrome.google.com/webstore/detail/browsermeta/dohofpanohcaolhaidnjcpnnadggdjbl" rel="noopener noreferrer"&gt;You can download Browsermeta extension from chrome web store.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can not only see the browser and its version that the client is using, but also a screenshot of where they are currently on the page, along with information about their screen, OS, stylesheets, scripts, browser extensions that are current in use and current URL. Other than the basic information, clients can choose not to share a screenshot, screen information, stylesheets, scripts, browser extensions and current URL.&lt;/p&gt;

&lt;p&gt;The extension and the website are in its very early and beta stages. It is completely free and adheres firmly to keep the privacy of the clients/users safe. We plan on adding tons of features like error monitoring, authentication, private links and give users a general and absolute control over their data.&lt;/p&gt;

&lt;p&gt;However, keep in mind that most of the data that you find in the 'meta sessions' are generally available in the browser. The problem is that the client cannot simply go to the inspect tools to collect all the information or give you a list of the extensions they are using. Even as a developer, it can be hard to look for all the script tags, stylesheets and other such things for debugging.&lt;/p&gt;

&lt;p&gt;We'd love it if you could check the extension, use it and provide us with valuable feedback. Browsermeta is in its early beta stages. We plan on adding many features to it.&lt;/p&gt;

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

</description>
      <category>javascript</category>
      <category>browser</category>
      <category>extension</category>
      <category>debugging</category>
    </item>
    <item>
      <title>How I became a web developer in a year without a degree, a bulletproof method.</title>
      <dc:creator>Muhammad Hasnain</dc:creator>
      <pubDate>Mon, 01 Feb 2021 18:52:24 +0000</pubDate>
      <link>https://forem.com/hasnaindev/how-i-became-a-web-developer-in-a-year-without-a-degree-a-bulletproof-method-33b1</link>
      <guid>https://forem.com/hasnaindev/how-i-became-a-web-developer-in-a-year-without-a-degree-a-bulletproof-method-33b1</guid>
      <description>&lt;h1&gt;
  
  
  Table of Contents
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Preface&lt;/li&gt;
&lt;li&gt;Need for Clarity&lt;/li&gt;
&lt;li&gt;Inverse Goal Setting (The Bulletproof Method)&lt;/li&gt;
&lt;li&gt;Consistency + Time&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Preface
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In life you need either inspiration or desperation. ~ Tony Robbins&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Three months into my first semester I had to leave the university as it was clear we couldn't afford it. My father lost his business and we essentially became poor. With no education and a degree, I was rejected from a ton of places. I am a good teacher, I am really good with computers but non of that mattered.&lt;/p&gt;

&lt;p&gt;In Pakistan, parents pay for their children's education until they get a degree. You cannot earn enough without a degree or get student loans either. Without any degree your chances of having a good and respectable job or a status in society are pretty low.&lt;/p&gt;

&lt;p&gt;Looking at my peers, getting rejected and even being kicked out of an internship hurt me and made me angry too. It was a really tough time in my life where I lost, not only the chance to get a good education but friends too and I became extremely depressed.&lt;/p&gt;

&lt;p&gt;I had made a decision nevertheless and I committed to it. I used all my "negative emotions" to drive me towards my goals. "Revenge" aside, in the end, it is all about having a better life not only for yourself but for your family too.&lt;/p&gt;

&lt;p&gt;I did became a web developer and now I have one year and eight months of total experience. I do not possess any degree and or certifications.&lt;/p&gt;

&lt;p&gt;Today, I'm going to share with you how I became a web developer in a year without any degree. What I'm about to share does not only applies to this field but to all other engineering and technical fields. Usually, in these fields employees require you to have a certain set of technical skills and these skills are measurable, both quantitively and qualitatively.&lt;/p&gt;

&lt;h2&gt;
  
  
  Clarity
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The best time to have a map is before you enter the woods. ~ Brendon Burchard&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Clarity is perhaps one of the most important thing you can have in your life. When you are clear about what is it that you exactly want, you have an edge over other people. You can divert all of your attention and energy towards the attainment of that goal.&lt;/p&gt;

&lt;p&gt;Remember, this concept of clarity that we're talking about will not only make you a web developer but also a better person. This is an invitation to you to lead a life that has been examined rather than the one that is reactive and lived according to what other people expect of you.&lt;/p&gt;

&lt;p&gt;Be precisely clear on what you want because a lot of people climb the proverbial ladder of success, only to realize that it was leaning against the wrong wall.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inverse Goal Setting (The Bulletproof Method)
&lt;/h2&gt;

&lt;p&gt;Do you want to become a web developer? Let's suppose you want to become a front-end developer. What you need to do is to google this term, "front-end jobs" and you will get a list of front-end jobs, easy as that.&lt;/p&gt;

&lt;p&gt;What you need to do after that is to click on the job posts and look at the skills they're expecting from their candidates. Usually they'll contain things like HTML, CSS, JavaScript, SASS, ReactJS/VueJS/AngularJS. Write these skills down.&lt;/p&gt;

&lt;p&gt;Go through as many posts as you can and write all the skills they require. Some of them may contain something unique and different, like a project management tool or a certain library. You may write those down too without any worry.&lt;/p&gt;

&lt;p&gt;After that what you would need to do is to look for a pattern. What set of skills do you see being repeated in almost all of the job posts? For us, these skills should be repeated, "HTML, CSS, JavaScript, SASS and ReactJS." You may ignore things like RxJS, Bugherd or other such tools and libraries which are just mentioned a handful of times.&lt;/p&gt;

&lt;p&gt;At this point, you should've figured out the pattern. The tools and languages that are essential for you to learn in order to land that specific job.&lt;/p&gt;

&lt;p&gt;What we just did is what I call, "Inverse Goal Setting." I was thinking of a clever name but honestly, this is much better than "Inverse Back Tracking." I'm cringing really hard right now, haha.&lt;/p&gt;

&lt;p&gt;Anyways, now that you know what skills you need, you can start your research on what to learn first and what comes latter. You make a map of sorts. In our example, we want to become a front-end developer. I would start with HTML and CSS and learn them before heading into SASS which is an intermediate concept. I will start with JavaScript before I head into TypeScript, Angular or React which are advanced tools and libraries.&lt;/p&gt;

&lt;p&gt;As soon as you're done with that, you need to start taking action! Udemy, YouTube, e-books, FreeCodeCamp all of these platforms contain all the knowledge you need to become a front-end or back-end developer.&lt;/p&gt;

&lt;p&gt;Study, make lots and lots of projects on your own. Sure, there are a lot of code-along project videos that you can watch and code along with but you also need to create something on your own. You need to take the concepts and apply them as you understand them, it will help you learn more than anything else.&lt;/p&gt;

&lt;p&gt;After you learn most of the things that were on your list and you have projects online and your code is on Github, you are pretty much ready to start applying for jobs. Since, this isn't an interview advice post, I am not going to discuss data structures, algorithms, clean code etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Consistency + Time
&lt;/h2&gt;

&lt;p&gt;Consistency is the key to mastery! If you want to become good at something, you need to spent large chunks of your time doing that thing on a daily basis, even when no one is looking and especially when no one is looking!&lt;/p&gt;

&lt;p&gt;Ronaldo used to wear weights on his ankles and kick football for hours. Elon Musk used to work 120 hours in a single week and no wonder he is running multiple companies. There are tons of other such success examples.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is in the moments of decisions that our destiny is shaped. ~ Tony Robbins&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;All such success stories have similar patterns, certain habits that they all had. They decided what they wanted, they trained and worked for hundreds of hours every single week or month for years. They trained when no one was looking, they trained when they felt like giving up and became depressed.&lt;/p&gt;

&lt;p&gt;I had similar experience too. Studying for 12+ hours each day there were days when I completely burnt up and when I became depressed and really uncertain at times and questioned myself but I had made my mind and I was going to do it. It may become extremely hard at times but you keep at it anyway.&lt;/p&gt;

&lt;p&gt;It is your decision to work hard on a daily basis that separates you from the crowd. In the end, the crowd will notice your success and cheer you on, they always do. Your critics becomes your supporters, but they will never see how hard you worked and that is okay because I think we're all like that unless we choose to gain awareness of self and choose to look closely and study successful people.&lt;/p&gt;

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

&lt;p&gt;Know what you want! If you want to be a front-end developer or back-end developer, android developer or a game developer. Checkout job posts for that specific title and note down all the skills, tools and libraries they want from their desired candidate. Figure out what set of skills, tools and libraries are being repeated, viola, you now have a pattern. Make a sensible plan and get to work! Work hard, be consistent and be patient.&lt;/p&gt;

&lt;p&gt;UPDATE: I'm really grateful to you all and very glad that this post served as an inspiration to many. This is my &lt;a href="https://www.linkedin.com/in/muhammad-hasnain-5b5484194/" rel="noopener noreferrer"&gt;Linkedin&lt;/a&gt; profile if you guys want to connect. Have a great day!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>productivity</category>
      <category>performance</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Easiest way to convert HTMLCollection into an Array!</title>
      <dc:creator>Muhammad Hasnain</dc:creator>
      <pubDate>Mon, 11 Jan 2021 12:03:02 +0000</pubDate>
      <link>https://forem.com/hasnaindev/easiest-way-to-convert-htmlcollection-into-an-array-bkl</link>
      <guid>https://forem.com/hasnaindev/easiest-way-to-convert-htmlcollection-into-an-array-bkl</guid>
      <description>&lt;p&gt;There are two ways in which you can convert an HTMLCollection or a NodeList into an array.&lt;/p&gt;

&lt;p&gt;If you don't know what an HTMLCollection and a NodeList is, or why you would need to convert them into a normal Array, hold tight, I'll explain them soon enough! 🐢&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="c1"&gt;// First, pre-ES6 way.&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;htmlCollection&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="nf"&gt;getElementsByClassName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;btn&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;htmlCollection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&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;slice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elements&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Or you could do the same thing using ES6 syntax.&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;nodeList&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="nf"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.btn&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;nodeList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;nodeList&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Or use Array.from method as suggested by Traek Wells in comments. 😎&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;imageHtmlCollection&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="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;img&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;imageHtmlCollection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;htmlCollection&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// 🎉🎉&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is HTMLCollection and NodeList?
&lt;/h2&gt;

&lt;p&gt;When you use methods like &lt;code&gt;getElementsByClassName&lt;/code&gt; or &lt;code&gt;querySelectorAll&lt;/code&gt; they return an HTMLCollection or NodeList respectively instead of an Array.&lt;/p&gt;

&lt;p&gt;HTMLCollection contains HTML elements whereas NodeList goes a few steps further, it not only returns a list of HTML elements but can also return Nodes like TextNode and has additional methods list NodeList.forEach.&lt;/p&gt;

&lt;p&gt;For a more detailed discussion on the topic, you may read this stackoverflow post called: &lt;a href="https://stackoverflow.com/questions/15763358/difference-between-htmlcollection-nodelists-and-arrays-of-objects" rel="noopener noreferrer"&gt;The difference between HTMLCollection, NodeList and Array&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why convert them into an Array?
&lt;/h2&gt;

&lt;p&gt;The only reason to convert an HTMLCollection and NodeList to an Array is, if you want to use higher order functions like forEach, map, filter and reduce.&lt;/p&gt;

&lt;p&gt;There are a lot of use cases, for instance, let's say you have elements that contain a &lt;code&gt;data-src&lt;/code&gt; property along with &lt;code&gt;lazy-load&lt;/code&gt; class. If you want to access the &lt;code&gt;data-src&lt;/code&gt; and filter out all the elements that don't have &lt;code&gt;data-src&lt;/code&gt; or are empty, you may do the following.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Case
&lt;/h2&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;lazyLoadables&lt;/span&gt; &lt;span class="o"&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="nf"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.lazy-load&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;span class="nf"&gt;filter&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;data-src&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="nf"&gt;lazyLoadImages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lazyLoadables&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By doing this, you made sure only to pass the elements that have a source that needs to be loaded when it is required.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>html</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Custom Google Maps styles, with night mode! 🌒</title>
      <dc:creator>Muhammad Hasnain</dc:creator>
      <pubDate>Wed, 06 Jan 2021 10:50:25 +0000</pubDate>
      <link>https://forem.com/hasnaindev/custom-google-maps-styled-with-night-mode-5a87</link>
      <guid>https://forem.com/hasnaindev/custom-google-maps-styled-with-night-mode-5a87</guid>
      <description>&lt;p&gt;Dark mode has become a trend in modern day websites (pun intended, i'm so funny 🤪). Recently, I worked on a project where we had a highly customized Google Map. There were a lot of Map and DOM interactions along with styling. A lot went into it. 🥴&lt;/p&gt;

&lt;p&gt;While studying how to style Google Maps, I stumbled across a very good resource that gives you readymade Google Maps styles, including &lt;code&gt;Night Mode&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Introducing, &lt;a href="https://mapstyle.withgoogle.com/" rel="noopener noreferrer"&gt;Google Maps Styling Wizard&lt;/a&gt;! 🎊🎉&lt;/p&gt;

&lt;h2&gt;
  
  
  How To 🐱‍👓
&lt;/h2&gt;

&lt;p&gt;Literally, select any style you want and click on the &lt;code&gt;Finish&lt;/code&gt; button. It gives styles for the selected map theme in JSON format. After that, all you have to do is this.&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;const&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;google&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;maps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Map&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="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[...],&lt;/span&gt; &lt;span class="c1"&gt;// Paste your styles here!&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course, make sure to set appropriate zoom level, position coordinates and API keys etc.&lt;/p&gt;

&lt;p&gt;PS, the JSON formats are also a good way to learn how to style your maps. For more info, look into the official &lt;a href="https://developers.google.com/maps/documentation/javascript/styling" rel="noopener noreferrer"&gt;Google Map styling guide&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>html</category>
    </item>
    <item>
      <title>Easiest way to convert JavaScript object to map!</title>
      <dc:creator>Muhammad Hasnain</dc:creator>
      <pubDate>Fri, 01 Jan 2021 16:05:30 +0000</pubDate>
      <link>https://forem.com/hasnaindev/easiest-way-to-convert-javascript-object-to-map-4omh</link>
      <guid>https://forem.com/hasnaindev/easiest-way-to-convert-javascript-object-to-map-4omh</guid>
      <description>&lt;div class="highlight js-code-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;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hasnain&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;profession&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Web Developer&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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When and why to use Map over Object? 🤔&lt;/p&gt;

&lt;p&gt;There are a lot of excellent people like you, with tons of knowledge out there writing awesome answers! Here is an amazing &lt;a href="https://stackoverflow.com/questions/18541940/map-vs-object-in-javascript" rel="noopener noreferrer"&gt;stackoverflow&lt;/a&gt; post I found that explains just that! 🎉🎊&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;You are lazy&lt;/strong&gt; 🥴
&lt;/h3&gt;

&lt;p&gt;I know you are lazy like me (every good developer should be), so I'd like to briefly describe the difference between Object and Map. With that information, be the judge of when to use which! 😎&lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" rel="noopener noreferrer"&gt;Mozilla&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;A Map's key can be any value, including primitive types, objects and functions.&lt;/li&gt;
&lt;li&gt;You can use &lt;code&gt;for...of&lt;/code&gt; loop to iterate Map in the insertion order, meaning what you first inserted will be the first element to show up in the loop and so on.&lt;/li&gt;
&lt;li&gt;Just as Array have &lt;code&gt;length&lt;/code&gt; property, Map comes with a &lt;code&gt;size&lt;/code&gt; property.&lt;/li&gt;
&lt;li&gt;Performs better when a lot of insertion and removal operations are involved.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>react</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
