<?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: Enmanuel Durán</title>
    <description>The latest articles on Forem by Enmanuel Durán (@duranenmanuel).</description>
    <link>https://forem.com/duranenmanuel</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%2F136794%2F2bbb8b0d-f6d3-4537-bafe-3d5545743ccc.jpg</url>
      <title>Forem: Enmanuel Durán</title>
      <link>https://forem.com/duranenmanuel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/duranenmanuel"/>
    <language>en</language>
    <item>
      <title>Understanding Big O Notation as a Software Developer</title>
      <dc:creator>Enmanuel Durán</dc:creator>
      <pubDate>Fri, 30 Apr 2021 13:51:13 +0000</pubDate>
      <link>https://forem.com/duranenmanuel/understanding-big-o-notation-as-a-software-developer-4mpg</link>
      <guid>https://forem.com/duranenmanuel/understanding-big-o-notation-as-a-software-developer-4mpg</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted on &lt;a href="https://enmascript.com/articles/2021/04/29/understanding-big-o-notation" rel="noopener noreferrer"&gt;Enmascript.com&lt;/a&gt;, for a better reading experience click the link to the original post.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Big O&lt;/strong&gt; is a highly popular notation that is often heard in the world of algorithms, performance and optimization. It is a pivotal topic for coding interviews at big companies such as Google, Amazon, Facebook... everybody says you should learn about it, but why? why would you or anyone learn about Big O if you are not taking interviews? In this article, I will try to explain from my perspective what Big O is, why it is useful and how you can apply it in an everyday basis to make you improve as a software developer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Definition
&lt;/h2&gt;

&lt;p&gt;There are multiple ways of writing algorithms, you can implement a feature using different approaches, everybody has a unique way of thinking and solving their way through problems, for this reason, we need a way to understand how these different solutions perform under different scenarios to help us determine which one works better based on our needs, &lt;strong&gt;Big O&lt;/strong&gt; is a notation that allows us to evaluate this, specifically it allows us to evaluate growth rates by analyzing how &lt;strong&gt;time complexity&lt;/strong&gt; (Time of execution) and &lt;strong&gt;space complexity&lt;/strong&gt; (memory usage) scale for a given algorithm when larger input sizes are processed by it. Big O is driven by the upper bound or higher complexity found in an algorithm so the worst case complexity will be the Big O representation of your code..&lt;/p&gt;

&lt;p&gt;For a formal definition of Big O I would recommend you to read &lt;a href="https://en.wikipedia.org/wiki/Big_O_notation#Formal_definition" rel="noopener noreferrer"&gt;the one from wikipedia&lt;/a&gt; which is pretty straightforward, if you have trouble understanding the formal definition &lt;strong&gt;do not worry&lt;/strong&gt;, it is enough to understand the intuitive definition highlighted above.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding How to Evaluate Complexities
&lt;/h2&gt;

&lt;p&gt;To understand how to evaluate an algorithm or a piece of code we need to separate it into statements or operations and we need to understand how each of these affects the algorithm.&lt;/p&gt;

&lt;p&gt;Let's define some important concepts before proceeding:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Complexity&lt;/strong&gt; and &lt;strong&gt;Performance&lt;/strong&gt; are two different things, The former specifies how the required resources for a program scale and the latter specifies how much of those resources are used taking into consideration the environment (the machine) in which an algorithm is running, the code, the time... So Complexity affects Performance but this is unidirectional, performance &lt;em&gt;does not&lt;/em&gt; affect complexity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Statements&lt;/strong&gt; are the units or simple instructions executed in a program to perform an action, they could be in example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Defining a variable &lt;code&gt;let a = 1;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Making an arithmetic operation &lt;code&gt;3 + 2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;A Function call &lt;code&gt;fn()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are also &lt;strong&gt;Compound Statements&lt;/strong&gt; that contain more than one instruction or operation, one example is: &lt;code&gt;for (let i = 0; i &amp;lt; 10; i++) {}&lt;/code&gt; which is executing a loop but at the same time defining a variable &lt;code&gt;i&lt;/code&gt;, making a comparison &lt;code&gt;i &amp;lt; 10&lt;/code&gt; and incrementing the variable &lt;code&gt;i&lt;/code&gt; by 1 in each iteration.&lt;/p&gt;

&lt;p&gt;The idea is that you understand that programs are a set of &lt;strong&gt;statements&lt;/strong&gt; that determine their &lt;strong&gt;complexity&lt;/strong&gt;. In Big O specifically, we care about the statements with higher complexity, where "higher" means less efficient, so after evaluating all the statements the one that will define the complexity of the algorithm is the less efficient complexity found, so:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Big O&lt;/strong&gt; = &lt;strong&gt;max(&lt;/strong&gt;complexity(statement1), ..., complexity(statementN)&lt;strong&gt;)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally, two more concepts, as previously mentioned there are two types of complexities relevant to Big O:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt; evaluates how an algorithm will scale over time, helping us understand if it will be too slow or will be fast enough for our needs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt; evaluates how an algorithm makes use of space over its execution, for example, how many variables we are defining, how an array or object grows to ensure that we will not cause any issues with memory consumption given our needs...&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Constant
&lt;/h3&gt;

&lt;p&gt;Constant or O(1) Complexity refers to an algorithm that no matter the input size it will always take the same amount of time/space to perform a task (therefore it is constant when the input size grows), in example let's evaluate a function that validates if the first element of an array is a number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isFirstElementNumeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list&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;typeof&lt;/span&gt; &lt;span class="nx"&gt;list&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="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we evaluate this by statements we understand that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Line 1:&lt;/strong&gt; We are creating a function called &lt;code&gt;isFirstElementNumeric&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Line 1:&lt;/strong&gt; We are creating a variable called &lt;code&gt;list&lt;/code&gt; to hold the input.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Line 2:&lt;/strong&gt; We are returning a value&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Line 2:&lt;/strong&gt; We are getting the &lt;code&gt;typeof list[0]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Line 2:&lt;/strong&gt; We are making a comparison against &lt;code&gt;number&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice how I have separated the lines into multiple statements, one line can have multiple statements, and some of those statements can be native implementations of the language like in this case when &lt;code&gt;typeof&lt;/code&gt; is used, and this is important to understand, the inner implementation of native functionalities affects the complexity of an algorithm depending on how it is implemented by the language, so if you are evaluating an algorithm, you need to be careful to understand and account for the complexity of the inner works of the language.&lt;/p&gt;

&lt;p&gt;If we evaluate the Time Complexity of the previous algorithm knowing the concept of &lt;em&gt;constant&lt;/em&gt; we can determine that each statement is O(1), why? because for all the statements of this function no matter how big the input is, it will always take the same amount of time to evaluate every statement of the function, because in this case even if the array has 1000 elements we are just taking index &lt;strong&gt;0&lt;/strong&gt; and same goes for space, we are not defining new variables when executing this function, &lt;strong&gt;and this next part is important&lt;/strong&gt;, yes, we are using a variable &lt;code&gt;list&lt;/code&gt; to hold the input but when evaluating space complexity we don't take into consideration the input itself because that is the very premise upon which the algorithm evaluation itself is based on, if we considered the input, then, for any function receiving a data structure capable of storing multiple properties it would be at least linear time (We will explain linear time in a bit).&lt;/p&gt;

&lt;p&gt;The graph below represents how time complexity (Y Axis) gets affected when the input size (X Axis) grows, since time is constant time remains unchanged for any given input size.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%281%29.svg%23image-type%3Dno-border%3Bwidth%3Dauto" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%281%29.svg%23image-type%3Dno-border%3Bwidth%3Dauto"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-life usage of constant complexity
&lt;/h3&gt;

&lt;p&gt;Pretty much everywhere, i.e:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arithmetic operations&lt;/li&gt;
&lt;li&gt;Variable definitions&lt;/li&gt;
&lt;li&gt;Boolean Comparisons&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Logarithmic
&lt;/h3&gt;

&lt;p&gt;Also known as O(log(n)), As the name specifies, we determine a logarithmic time complexity when an algorithm runs in a time that is proportional to the logarithm of the input size as its size grows.&lt;/p&gt;

&lt;p&gt;Logarithms can have different basis, so for example for a log of base 10:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;log(1) = 0&lt;/li&gt;
&lt;li&gt;log(2) = ~0.301&lt;/li&gt;
&lt;li&gt;log(3) = ~0.477&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;and so on... As you can see the growth rate is still small and not linear. Usually, we are able to identify an algorithm that is O(log(n)) because it will divide itself into smaller and smaller pieces with each iteration or operation, the simplest example I can think of is the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;showIteratedValues&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&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;list&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 we evaluate this statement by statement we realize that lines 1,2 and 3 are constant time and in space, because we are defining a function and some variables and they do not change in respect of the input &lt;code&gt;n&lt;/code&gt;, but when we reach the while statement things get interesting.&lt;/p&gt;

&lt;p&gt;The while statement creates a loop based on the condition &lt;code&gt;i &amp;lt; n&lt;/code&gt;, so the cycle will execute until that condition is false, this already takes more than constant time to execute, why? because for a bigger input &lt;code&gt;n&lt;/code&gt; passed to this function the algorithm will take a longer time to execute because the while loop will have to potentially execute more iterations. With each iteration, the value of &lt;code&gt;i&lt;/code&gt; will &lt;em&gt;exponentially&lt;/em&gt; increase because we are multiplying &lt;code&gt;i&lt;/code&gt; by 2 and in the next iteration the result * 2 and so on (you might be thinking "wait, exponentially?, aren't we talking about logarithmic time here?") yes, this is true but something important to notice is that logarithmic growth is the inverse of exponential growth meaning that if the loop's variable condition is increasing exponentially then the number of executions needed by the loop to finish decreases logarithmically, hence the &lt;strong&gt;time complexity&lt;/strong&gt; is logarithmic.&lt;/p&gt;

&lt;p&gt;How about &lt;strong&gt;space complexity&lt;/strong&gt;? Good question, let's evaluate the statements in the loop:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;i = i * 2;&lt;/code&gt; for the definition of the variable &lt;code&gt;i&lt;/code&gt; inside of the while loop we can say that the space complexity is constant, why? because with each iteration the value of &lt;code&gt;i&lt;/code&gt; is overwritten with the new value of &lt;code&gt;i * 2&lt;/code&gt;, so this is not growing in space.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;list.push(i)&lt;/code&gt; this right here is the key to the space complexity of this algorithm, every time an iteration happens the array &lt;code&gt;list&lt;/code&gt; grows in parallel with time complexity, hence the space complexity of this statement is logarithmic.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hence &lt;strong&gt;space complexity&lt;/strong&gt; is logarithmic.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28log%28n%29%29.svg%23image-type%3Dno-border%3Bwidth%3Dauto" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28log%28n%29%29.svg%23image-type%3Dno-border%3Bwidth%3Dauto"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-life usage of logarithmic complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Binary Search.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Linear
&lt;/h3&gt;

&lt;p&gt;One of the easiest complexities to identify along with &lt;em&gt;constant&lt;/em&gt;, an algorithm has a linear O(n) complexity when it grows proportionally to the input, in other words when the growth rate is fixed when iterating over each input given:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;elevateToPowerOfTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list&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;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&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;powerOfTwoList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

    &lt;span class="k"&gt;for &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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&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="nx"&gt;powerOfTwoList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="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's evaluate by statements again:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The first three lines of code are constant time and space, they are not changing for a bigger input.&lt;/li&gt;
&lt;li&gt;for the for loop we have &lt;code&gt;for (let i = 0; i &amp;lt; n; i++) {&lt;/code&gt; which contains 4 statements:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;let i = 0;&lt;/code&gt; which will get overwritten next time when it is incremented so it is constant.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;i &amp;lt; n&lt;/code&gt; a simple comparison constant.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;i++&lt;/code&gt; increments &lt;code&gt;i&lt;/code&gt; by 1 on each iteration, the value of i changes but the variable &lt;code&gt;i&lt;/code&gt; is overwritten.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;for&lt;/code&gt; statement itself, we will need to iterate over &lt;em&gt;each&lt;/em&gt; value until &lt;code&gt;i &amp;lt; n&lt;/code&gt; is no longer true, if &lt;code&gt;n&lt;/code&gt; is a higher value we will have to iterate as many times as needed again until &lt;code&gt;i&lt;/code&gt; reaches a value &lt;code&gt;&amp;gt;= n&lt;/code&gt; and this is true of any &lt;code&gt;n&lt;/code&gt; for this algorithm, as you can notice in linear time the correlation of the input size with the runtime is clear, if we increase &lt;code&gt;n&lt;/code&gt; by a factor of 2 we already know that we will have to iterate twice as many times as before.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For &lt;strong&gt;Space complexity&lt;/strong&gt; we can proceed to evaluate the next statement:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;powerOfTwoList.push(i**2);&lt;/code&gt; which increases the array with each iteration until the loop is over, since the loop itself is linear time this array will grow in space at the same rate hence space complexity is linear.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;if we compare this with logarithmic time complexity we will notice that logarithmic time is a bit more ambiguous, nevertheless, it is worth noting that logarithmic time is more efficient than linear because logarithmic time grows at a slower rate (due to the partitioning that happens through each iteration, thing that does not happen in linear time).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28n%29.svg%23image-type%3Dno-border%3Bwidth%3Dauto" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28n%29.svg%23image-type%3Dno-border%3Bwidth%3Dauto"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-life usage of Linear complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;loops&lt;/li&gt;
&lt;li&gt;Recursive Functions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Linearithmic
&lt;/h3&gt;

&lt;p&gt;Linearithmic complexity is a combination of linear and logarithmic complexity hence O(n * log(n))&lt;/p&gt;

&lt;p&gt;Linearithmic algorithms are slower than O(n) algorithms but still better than quadratic time algorithms (which we will see in a bit) a way to see them iteratively speaking is by combining our linear and logarithmic code examples, to end up with a linear cycle executing a logarithmic one inside so that:&lt;/p&gt;

&lt;p&gt;O(n) * O(log(n)) = O(n * log(n))&lt;/p&gt;

&lt;p&gt;But this is just one way to express it in code, linearithmic algorithms have many ways of presenting themselves recursively and by input division over time...&lt;/p&gt;

&lt;p&gt;Following the iterative idea from before we could write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;logarithmicExecutions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&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;logarithmicPossibilities&lt;/span&gt; &lt;span class="o"&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;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;counter&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="nx"&gt;logarithmicPossibilities&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&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;Evaluating the code above and already having evaluated linear and logarithmic time complexity it becomes even clearer why it is linearithmic, however, the space complexity of this piece of code is not linearithmic, if you take a look closely it is linear space complexity given that &lt;code&gt;logarithmicPossibilities&lt;/code&gt; grows proportionally to the outer loop which is o(n) and the rest of the variables are overwritten per iteration.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28nlog%28n%29%29.svg%23image-type%3Dno-border%3Bwidth%3Dauto" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28nlog%28n%29%29.svg%23image-type%3Dno-border%3Bwidth%3Dauto"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-life usage of Linearithmic complexity
&lt;/h3&gt;

&lt;p&gt;they are usually found in sorting algorithms such:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Merge Sort&lt;/li&gt;
&lt;li&gt;Heap Sort&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Quadratic
&lt;/h3&gt;

&lt;p&gt;An algorithm ison quadratic time or space complexity O(n^2) when it grows proportionally to the squared value of the input given so:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For 1 we define 1^2 = 1&lt;/li&gt;
&lt;li&gt;For 2 we define 2^2 = 4&lt;/li&gt;
&lt;li&gt;For 3 we define 3^2 = 9&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and so on... visually speaking quadratic time would look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28n2%29.svg%23image-type%3Dno-border%3Bwidth%3Dauto" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28n2%29.svg%23image-type%3Dno-border%3Bwidth%3Dauto"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;a good example of this type is when we are dealing with nested loops (one level of nesting), it does not mean that all nested loops are quadratic by any means, and I will explain this better below, but a typical case could be the next one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const list = [1,2,3];
let total = 0;

for (let i = 0; i &amp;lt; list.length; i++) {
    total += i;

    for (let j = 0; j &amp;lt; list.length; j++){
        total += j;
    }
}

console.log(total);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you take a look at the code above you'll see that we have an outer loop that executes up to n times where n = 3 (the length of &lt;code&gt;list&lt;/code&gt;), inside of that loop we have another one that is doing the same thing, which means that for each element on the parent loop we are executing n times the inner loop:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-n2.svg%23image-type%3Dno-border%3Bwidth%3Dauto" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-n2.svg%23image-type%3Dno-border%3Bwidth%3Dauto"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So basically n * n hence n^2, however not all nested loops represent a quadratic time complexity, this only happens in cases where the two loops (outer and nested loop) are running the same amount of n times, in the code example above using &lt;code&gt;list.length&lt;/code&gt; as n, but what happens if one of the loops has a different variable with a different size? - well in that case the complexity would be O(n * m), because both variables are distinct.&lt;/p&gt;

&lt;p&gt;As for the &lt;strong&gt;space complexity&lt;/strong&gt; of our algorithm above it is constant O(1) because memory is not increasing concering the input.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-life usage of quadratic complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Nested loops&lt;/li&gt;
&lt;li&gt;Bubble Sort&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Exponential
&lt;/h3&gt;

&lt;p&gt;Also known as O(a^n) where a is constant and n is variable, not to be confused with polynomial where it is the opposite.&lt;/p&gt;

&lt;p&gt;A simple example I can think of that takes &lt;strong&gt;exponential&lt;/strong&gt; time is recursive &lt;em&gt;Fibonacci&lt;/em&gt; without memoization (If you don't know what memoization is take a look at one of my previous articles &lt;em&gt;&lt;a href="https://dev.to/articles/2019/04/22/avoid-recomputing-heavy-tasks-by-leveraging-memoization-in-javascript"&gt;Avoid recomputing heavy tasks by leveraging memoization in javascript&lt;/a&gt;&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;let's take a look at the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fibonacci(num) {
    if (num &amp;lt;= 1) {
        return 1;
    }

    return fibonacci(num - 1) + fibonacci(num - 2);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so why is it exponential? I think it is useful to look at a visualization to understand what is happening here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-26-fibonacci.png%23image-type%3Dno-border%3Bwidth%3Dauto" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-26-fibonacci.png%23image-type%3Dno-border%3Bwidth%3Dauto"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For each new function call, the algorithm doubles the previous amount of executions which tells us that the exponent n is increasing with each recursive call in this case for base 2.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28nn%29.svg%23image-type%3Dno-border%3Bwidth%3Dauto" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28nn%29.svg%23image-type%3Dno-border%3Bwidth%3Dauto"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; There is a theorem that helps us determine the complexity of recursive algorithms called &lt;em&gt;Master Theorem&lt;/em&gt; but it deserves its own article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-life usage of Exponential complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Exponential algorithms in the form of O(a^n) which &lt;em&gt;usually&lt;/em&gt; solve a problem of size n by recursively solving a smaller problems of size n-1.&lt;/li&gt;
&lt;li&gt;Fibonacci sequence.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Factorial
&lt;/h3&gt;

&lt;p&gt;Factorial time O(n!) is not an indicator of good performance but sometimes we can't do better than that, to put you into context let's remember its definition: The factorial of a non-negative integer number n is the product of all positive integers &lt;strong&gt;&lt;em&gt;less than&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;equal to&lt;/em&gt;&lt;/strong&gt; n.&lt;/p&gt;

&lt;p&gt;Factorial algorithms are often found when making permutations and combinations.&lt;/p&gt;

&lt;p&gt;so for example, the factorial of 4:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4! = 4 * 3 * 2 * 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Real-life usage of Factorial complexity
&lt;/h3&gt;

&lt;p&gt;A classic case scenario of this time complexity is the processing of every possible permutation of an array where n is the size of the array and so it is n! because we need to make all the permutations possible up to the length of n.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Permutations&lt;/li&gt;
&lt;li&gt;Combinations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28nfactorial%29.svg%23image-type%3Dno-border%3Bwidth%3Dauto" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28nfactorial%29.svg%23image-type%3Dno-border%3Bwidth%3Dauto"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Due to complexity I have decided that I will write a separated article to explain one example of factorial complexity.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hierarchy
&lt;/h3&gt;

&lt;p&gt;So after everything we have learned, we can define that for an n large enough it holds that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(1) &amp;lt; O(log(n)) &amp;lt; O(n) &amp;lt; O(n * log(n)) &amp;lt; O(n^2) &amp;lt; O(2^n) &amp;lt; O(n!)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which means that if you are evaluating your code and you find that it has an statement that is O(n) but there is another block of code that is O(n^2) then your code is O(n^2) because as previously mentioned, Big O cares about the upper bound.&lt;/p&gt;

&lt;p&gt;There are other complexities that we are not including in this article like in example cubic complexity, but the idea is not to name them all, instead, I want to help you understand how to analyze your code to determine how efficient it is and of course dig deeper into the topic if you are interested.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply this knowledge in your day to day basis
&lt;/h2&gt;

&lt;p&gt;One way to start practicing Big O notation is by thinking on the complexity of the problems you are solving, go back to problems you have previously solved, and see for fun if you can determine their complexity and if there is room for improvement for a future state, this is a very useful practice that will help you practice statement's complexity evaluation so that it becomes easier and easier, however, this does not mean that you need to refactor all your code, NO!, before doing improvements, make sure to measure if it is a real problem given the use case of the algorithm, don't apply blind micro-optimizations since that is not a good practice either, I have written an article on that matter called &lt;em&gt;&lt;a href="https://dev.to/articles/2019/06/04/code-quality-and-web-performance-the-myths-the-dos-and-the-donts"&gt;Code quality and web performance, the myths, the do's and the don'ts&lt;/a&gt;&lt;/em&gt; in case you wanna check it out.&lt;/p&gt;

&lt;p&gt;When making a new implementation think in terms of scalability, feel free to do a naive implementation first and apply gradual improvements from there or take your time thinking on the implementation to get it right the first time, either way works, the important part is to think in terms of how each statement you are adding affects the algorithm as a whole.&lt;/p&gt;

&lt;p&gt;Code reviews are also a perfect place to put this into practice, understand the business application of the code that you are reviewing but also the complexity that is being added to the codebase and see &lt;em&gt;if&lt;/em&gt; it can be improved and if it is worth it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing Thoughts
&lt;/h2&gt;

&lt;p&gt;I have to say, writing this article was a lot of fun, I hope that I was able to incentivize you to keep learning about algorithms and their complexities, this is just a little part of everything that involves algorithm's complexities, in example besides Big O (&lt;strong&gt;O&lt;/strong&gt;) we also have Big Omega (\Omega) to evaluate the lower bound, and Big Theta (\Theta) to evaluate the average case, so please do not stop here, if you have any comments or wanna reach out feel free to do it, as always the information is below.&lt;/p&gt;

&lt;p&gt;See you next time!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>computerscience</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Understanding Singly Linked Lists and some of their uses</title>
      <dc:creator>Enmanuel Durán</dc:creator>
      <pubDate>Mon, 12 Apr 2021 15:16:10 +0000</pubDate>
      <link>https://forem.com/duranenmanuel/understanding-singly-linked-lists-and-some-of-their-uses-39f2</link>
      <guid>https://forem.com/duranenmanuel/understanding-singly-linked-lists-and-some-of-their-uses-39f2</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted on &lt;a href="https://enmascript.com/articles/2021/04/12/understanding-singly-linked-lists-and-some-of-their-uses" rel="noopener noreferrer"&gt;Enmascript.com&lt;/a&gt;, for a better reading experience go to the original post.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Definition
&lt;/h2&gt;

&lt;p&gt;Linked Lists are a fundamental data structure used to store information linearly, this information is not stored in contiguous places in memory rather linked lists use a pointer in each node that links to the next node that is stored.&lt;/p&gt;

&lt;p&gt;A node in a linked list could be represented as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-03-singly-linked-list-node.svg%23image-type%3Dno-border%3Bwidth%3Dauto" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-03-singly-linked-list-node.svg%23image-type%3Dno-border%3Bwidth%3Dauto"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;which in code could be represented as:&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;class&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&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;next&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="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;Allowing us to create a new node like:&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;node&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;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Node value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As already stated each of these nodes contains data that is stored in &lt;code&gt;this.value&lt;/code&gt; and has a direct reference to the next node in line through &lt;code&gt;this.next&lt;/code&gt;, the first node of the linked list is usually referred to as the Head and the last node is called Tail, since the Tail is always the last node the value of its &lt;code&gt;this.next&lt;/code&gt; property will always be &lt;code&gt;null&lt;/code&gt; for singly linked lists.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-03-singly-linked-list.svg%23image-type%3Dno-border" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-03-singly-linked-list.svg%23image-type%3Dno-border"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So if we try to represent that in code we get something close to:&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;class&lt;/span&gt; &lt;span class="nc"&gt;LinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&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;head&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  1. Appending nodes to a linked list
&lt;/h2&gt;

&lt;p&gt;Let's start by creating a linked list API, we already know how to represent a node and we know the basics, we know how we will represent the head and the tail, so we can start to define how we will append new nodes to the linked list, for this we need to consider that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If not head is defined the new node should be defined as the head and also the tail.&lt;/li&gt;
&lt;li&gt;If the head is defined we add the new node to the current tail's &lt;code&gt;next&lt;/code&gt; property and we define the new node as the tail.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Node from './LinkedListNode';

class LinkedList {
    constructor() {
        this.head = null;
        this.tail = null;
    }

    append(value) {
        // We create a new Node
        const node = new Node(value);

        if (!this.head) {
            // If not head is define we define it alongside with the tail
            this.head = node;
            // We  define the tail
            this.tail = node;

            return this;
        }

        // If the head is defined we attach the new node to the
        // tail's next property
        this.tail.next = node;
        // We make the new node the tail
        this.tail = node;

        return this;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now if we call append like:&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;linkedList&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;LinkedList&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;linkedList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&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="nx"&gt;linkedList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;linkedList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We would get this result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-04-singly-linked-list-value.svg%23image-type%3Dno-border" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-04-singly-linked-list-value.svg%23image-type%3Dno-border"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and if we do something like &lt;code&gt;console.log(linkedList)&lt;/code&gt; you will get a result like:&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="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;head&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;value&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;next&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;value&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;next&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;value&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;next&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tail&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;value&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;next&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&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;This is excellent, you can see how the nodes connect with each other, they are just objects connected to each other through their &lt;code&gt;next&lt;/code&gt; property.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.1. Time complexity for Appending nodes to a linked list
&lt;/h3&gt;

&lt;p&gt;Appending an element to the end of the linked list requires us to modify the tail's &lt;code&gt;next&lt;/code&gt; property and reassign the tail with the value of the new node.&lt;/p&gt;

&lt;p&gt;this is true for any node we want to append which makes this a constant &lt;strong&gt;O(1)&lt;/strong&gt; operation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%281%29.svg%23image-type%3Dno-border-limied-width%3B" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%281%29.svg%23image-type%3Dno-border-limied-width%3B"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Prepending nodes to a linked list
&lt;/h2&gt;

&lt;p&gt;Prepending a node is simpler since we already have the &lt;code&gt;head&lt;/code&gt; of the list stored, the only thing we need to do is assign the new node as the head of the list and define its &lt;code&gt;next&lt;/code&gt; property with a reference to the previous head Node.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class LinkedList {
    ...

    prepend(element) {
        const previousHead = this.head;
        this.head = new Node(element);
        this.head.next = previousHead;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.1. Time complexity for prepending nodes
&lt;/h3&gt;

&lt;p&gt;It does not matter how many nodes the linked list has, it will always be the same process and complexity for prepending hence the time complexity of prepending is constant &lt;strong&gt;O(1)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%281%29.svg%23image-type%3Dno-border-limied-width%3B" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%281%29.svg%23image-type%3Dno-border-limied-width%3B"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Accessing and Searching nodes
&lt;/h2&gt;

&lt;p&gt;The only way to access and search an element in a given linked list is through the iteration of the &lt;code&gt;next&lt;/code&gt; property of all the nodes that come before the node we are looking for, it is important to note that if the element we are searching or trying to access is not found this would still require us to go through all the nodes in the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class LinkedList {
    ...

    find(value) {
        if (!this.head || value === undefined) {
            return null;
        }

        let currentNode = this.head;

        while(currentNode) {
            if (currentNode.value === value) {
                return currentNode;
            }

            currentNode = currentNode.next;
        }

        return null;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;i.e let's find the node 3 in the linked list below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-05-singly-linked-list-find.svg%23image-type%3Dno-border" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-05-singly-linked-list-find.svg%23image-type%3Dno-border"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3.1. Time complexity of accessing and searching nodes
&lt;/h3&gt;

&lt;p&gt;Knowing this we can stablish that accessing and searching an element would be &lt;strong&gt;O(n)&lt;/strong&gt; where &lt;strong&gt;n = number of nodes in the list&lt;/strong&gt;, even though we don't always search the whole list the big O notation analizes algorithms by their trend and worst case scenario and so we arrive at this conclusion.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28n%29.svg%23image-type%3Dno-border-limied-width%3B" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28n%29.svg%23image-type%3Dno-border-limied-width%3B"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Removing nodes from a linked list
&lt;/h2&gt;

&lt;p&gt;Great, now as you can imagine, removing elements from a linked list is pretty straight forward:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check if the node we want to remove is currently the head of our linked list, if so, we just remove the reference to such node by making &lt;code&gt;this.head&lt;/code&gt; be the next node in line (since now there is no reference to the node with value 1 it will be garbage collected and removed):&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-05-singly-linked-list-remove-head.svg%23image-type%3Dno-border" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-05-singly-linked-list-remove-head.svg%23image-type%3Dno-border"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;If the node to remove is not the head, we iterate over the nodes until the node to remove is found, if the node is not found we don't do anything.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the node to remove is found we get the node previous to that one, we then modify this previous node's  &lt;code&gt;next&lt;/code&gt; property so that it points to the node that comes after the node to remove, in this way the reference to the node to be removed is lost and it can be garbage collected hence the node is &lt;em&gt;removed&lt;/em&gt; from the linked list.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;let's see how this would look in the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class LinkedList {
    ...

    remove(value) {
        if (!this.head || value === undefined) {
            return null;
        }

        let nodeToRemove = null;

        // Check if the node to remove is the head node
        if (this.head.value === value) {
            // We save the node just to return it later
            nodeToRemove = this.head;
            // If the node is the head we remove the node by assigning
            // the second node as the head.
            this.head = this.head.next;
        } else {
            // currentNode will be used to iterate over the nodes
            let currentNode = this.head;

            // We iterate over the nodes until there are no more nodes left to search
            // or until we find the node to remove
            while(currentNode.next !== null) {
                if (currentNode.next.value === value) {
                    // We save the node just to return it later
                    nodeToRemove = currentNode.next;
                    // If we find the node we remove it as explained on point 4.
                    currentNode.next = currentNode.next.next;
                } else {
                    // If the node has not been found we continue searching
                    currentNode = currentNode.next;
                }
            }
        }

        return nodeToRemove;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's say we want to remove the node that contains the value 2, we would ideally do this by calling the method remove like:&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="nx"&gt;linkedList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which would modify the reference from the node with value 1 to be now the reference of the node with value 3, in this way node 2 is left out:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-04-singly-linked-list-remove.svg%23image-type%3Dno-border" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2021-04-04-singly-linked-list-remove.svg%23image-type%3Dno-border"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4.1. Time complexity for Deleting a node (From the beginning / Head node)
&lt;/h3&gt;

&lt;p&gt;Deleting a node from the beginning of the list as previously seen just requires us to change the &lt;code&gt;this.head&lt;/code&gt; value to be &lt;code&gt;this.head.next&lt;/code&gt; in this way we remove the reference to the first node, since this operation is constant no matter the size of the list it is considered &lt;strong&gt;O(1)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%281%29.svg%23image-type%3Dno-border-limied-width%3B" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%281%29.svg%23image-type%3Dno-border-limied-width%3B"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4.2. Time complexity for Deleting the Tail or any node that's not the head
&lt;/h3&gt;

&lt;p&gt;Doing this will require us to iterate over the list until we find the element to delete (Same that we need to search an node), then we just remove the node as usual so the time complexity would be &lt;strong&gt;O(n)&lt;/strong&gt; where &lt;strong&gt;n = number of nodes in the list&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28n%29.svg%23image-type%3Dno-border-limied-width%3B" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2Fo%28n%29.svg%23image-type%3Dno-border-limied-width%3B"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Space Complexity of linked lists
&lt;/h2&gt;

&lt;p&gt;The space required for a linked list is directly correlated with the number of nodes that it holds, this means that the more nodes we have the more space we use and this grows linearly per node which makes linked lists &lt;strong&gt;O(n)&lt;/strong&gt; for Space complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use cases and why to learn about them
&lt;/h2&gt;

&lt;p&gt;Most of the cases where linked lists shine come in situations where we need to insert or delete multiple nodes, in these cases linked lists perform at a constant time which makes them ideal, also since linked list's space grows linearly we can also leverage their dynamic memory allocation in ocassions where we lack memory.&lt;/p&gt;

&lt;p&gt;Another important point is that there are other structures that are and can be built with linked lists as a base, one good example are queues (That we will analyze later in another article)&lt;/p&gt;

&lt;p&gt;Hope this article helped you understand linked lists a little.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>computerscience</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Better ways of tracking data in your web application that you may not know of!</title>
      <dc:creator>Enmanuel Durán</dc:creator>
      <pubDate>Mon, 22 Mar 2021 14:57:00 +0000</pubDate>
      <link>https://forem.com/duranenmanuel/better-ways-of-tracking-data-in-your-web-application-that-you-may-not-know-of-3nn4</link>
      <guid>https://forem.com/duranenmanuel/better-ways-of-tracking-data-in-your-web-application-that-you-may-not-know-of-3nn4</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted on &lt;a href="https://enmascript.com/articles/2021/03/20/better-ways-of-tracking-data-in-your-web-application-that-you-may-not-know-of"&gt;Enmascript.com&lt;/a&gt;, for a better reading experience go to the original post.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Tracking data in our applications nowadays is pivotal to business growth, if done properly it can provide fundamental insights for an application that will allow us to understand how to improve it, data helps us understand our users which allows companies to understand their strengths and weaknesses better. Finally, good tracking marks the difference between success and failure, that's why getting it right is so important.&lt;/p&gt;

&lt;h2&gt;
  
  
  1.Why using XMLHttpRequest and Fetch is not always the right choice
&lt;/h2&gt;

&lt;p&gt;Usually, when we need to track actions in our applications in javaScript, we will make use of an &lt;a href="https://www.w3.org/TR/2012/NOTE-XMLHttpRequest1-20120117/#introduction"&gt;XMLHttpRequest&lt;/a&gt; or even better the &lt;a href="https://fetch.spec.whatwg.org/"&gt;Fetch API&lt;/a&gt;, this will allow us to make asynchronous requests which send valuable information to our servers or analytics services, this works very well for many types of tracking interactions, for example tracking when a user scrolled past a certain section of the page&lt;br&gt;
or checking the behavior of users to understand why a call to action may not be performing well.&lt;/p&gt;

&lt;p&gt;In summary, asynchronous requests are used constantly for tracking, and with reason, they allow us to send information quietly and in parallel to avoid interrupting the user's flow, but, there are some edge cases in which they might not be as efficient, like tracking an element when the page is about to get unloaded.&lt;/p&gt;

&lt;p&gt;Suppose that you want to track certain data and send it but the user decides to leave the page and go somewhere else, if your request is still in-flight the data will not reach the server because it will get canceled when going away from your page, so in essence:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WKKSZu4P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2021-03-16-fetch-tracking-example.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WKKSZu4P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2021-03-16-fetch-tracking-example.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first click generates a request and since it has enough time to be processed it ends up being successful, on the other hand the second click happens right before the page gets unloaded which causes the request to be interrupted and therefore canceled, this right here is the reason why using rudimentary asynchronous request can't ensure the resolution of tracking requests.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Why using redirects is not the golden standard
&lt;/h2&gt;

&lt;p&gt;Another popular way of tracking data nowadays is by leveraging redirects, this basically consists of a 3 steps process:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dUdoiqS2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2021-03-19-tracking-redirect.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dUdoiqS2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2021-03-19-tracking-redirect.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The user clicks a link, this link is not a direct connection to the destination URL, it is a link to an intermediary service.&lt;/li&gt;
&lt;li&gt;The intermediary service receives the request with the information, it then processes it and sends it over to each service that may need the data (this includes sending data to the tracking services)&lt;/li&gt;
&lt;li&gt;The user is finally redirected to the destination URL by the intermediary service.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This approach is good but it causes a delay in the user experience due to the redirection step that occurs synchronously, if the redirect takes long it may end up leading the user to leave the page, also if an unexpected error occurs in the redirecting layer the user might get stuck into this flow.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Enters the Beacon API
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://w3c.github.io/beacon/"&gt;Beacon API&lt;/a&gt; allows us to also send asynchronous requests to a web server but with a key difference, &lt;strong&gt;Beacon requests are &lt;em&gt;guaranteed&lt;/em&gt; to be sent&lt;/strong&gt;, yes, even after a page gets unloaded. This means that they are perfect to use for those cases in which:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You may want to track clicks on links.&lt;/li&gt;
&lt;li&gt;When sending pivotal data in moments where users leave the site.&lt;/li&gt;
&lt;li&gt;In cases in which you are not able to ensure the integrity of a normal asynchronous request.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To use the Beacon API it is very simple, you can access the function &lt;code&gt;sendBeacon&lt;/code&gt; through the &lt;code&gt;navigator&lt;/code&gt; object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const url = `https://myurl.com/api/tracking`;
const data = new Blob([JSON.stringify({ tracked: true })], {type : 'application/json; charset=UTF-8'});

const beaconQueued = navigator.sendBeacon(url, data);

if (beaconQueued) {
    // The request was queued
} else {
    // Something went wrong when trying to queue the request
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can send data as an &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"&gt;ArrayBufferView&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"&gt;ArrayBuffer&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob"&gt;Blob&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMString"&gt;DOMString&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/FormData"&gt;FormData&lt;/a&gt;, or &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams"&gt;URLSearchParams&lt;/a&gt; object. In the example above we are sending the information by using the Blob object, this is commonly used to send information to your server so you can manipulate the data like a JSON object.&lt;/p&gt;

&lt;p&gt;Some key concepts about the &lt;code&gt;navigator.sendBeacon&lt;/code&gt; function:&lt;/p&gt;

&lt;h3&gt;
  
  
  It returns a boolean
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;navigator.sendBeacon&lt;/code&gt; returns a boolean value, which means that the "Beacon" request was either enqueued (true) to be processed or not (false), this does not necessarily mean the request will be successful but as per the Beacon definition, the request is &lt;em&gt;guaranteed&lt;/em&gt; to be executed and completed.&lt;/p&gt;

&lt;h3&gt;
  
  
  It is executed after higher priority tasks
&lt;/h3&gt;

&lt;p&gt;After enqueued, the request will be executed &lt;em&gt;only&lt;/em&gt; after more important tasks have been processed, this is important to understand because this means that the request won't necessarily happen right away in some cases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Beacon Request Size Limit
&lt;/h3&gt;

&lt;p&gt;According to the documentation of the &lt;a href="https://www.w3.org/TR/beacon/#h-sec-processing-model"&gt;processing model for the Beacon API&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Requests initiated via the Beacon API automatically set the keepalive flag, and developers can similarly set the same flag manually when using the Fetch API. &lt;strong&gt;All requests with this flag set share the same in-flight quota restrictions that is enforced within the Fetch API.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and if we then read the &lt;a href="https://fetch.spec.whatwg.org/"&gt;Fetch API specs&lt;/a&gt; especially on section 4.6 it says the following:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If the sum of &lt;em&gt;contentLength&lt;/em&gt; and &lt;em&gt;inflightKeepaliveBytes&lt;/em&gt; is greater than 64 kibibytes, then return a network error.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;which means that the size limit is roughly 65.5 Kilobytes (just for general knowledge &lt;strong&gt;1 Kibibyte = 1.024 Kilobytes&lt;/strong&gt;)&lt;/p&gt;

&lt;p&gt;All this means that if we change a normal Fetch request for a Beacon based request, the following will occur:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eOMdH4a8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2021-03-16-fetch-tracking-beacon-example.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eOMdH4a8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2021-03-16-fetch-tracking-beacon-example.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see the request would not get canceled, it would get enqueued, then stalled for the necessary time and finally executed.&lt;/p&gt;

&lt;h2&gt;
  
  
  4.The Ping Attribute, a curious case
&lt;/h2&gt;

&lt;p&gt;Not many people know about this, but there is an HTML attribute used in anchor tags that is specifically created to help track information asynchronously in a non-blocking manner.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://html.spec.whatwg.org/multipage/links.html#ping"&gt;Ping attribute&lt;/a&gt; allows you to execute a POST request to an URL or list of URLs specified, it sends the word PING as the request payload. This property is not supported by all browsers but the fact that &lt;a href="https://html.spec.whatwg.org/multipage/links.html#ping"&gt;many popular browsers support it&lt;/a&gt; presents a great advantage.&lt;/p&gt;

&lt;p&gt;It is used in the following way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- For a single POST request URL --&amp;gt;
&amp;lt;a href="https://enmascript.com" ping="https://enmascript.com/api/tracking"&amp;gt;&amp;lt;/a&amp;gt;

&amp;lt;!-- For Multiple POST request URLS --&amp;gt;
&amp;lt;a
    href="https://enmascript.com"
    ping="https://enmascript.com/api/tracking https://enmascript.com/api/tracking-2"&amp;gt;
&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When clicking the first link above it will take the user as usual to &lt;a href="https://enmascript.com"&gt;https://enmascript.com&lt;/a&gt; but in parallel it will make an asynchronous POST request to &lt;a href="https://enmascript.com/api/tracking"&gt;https://enmascript.com/api/tracking&lt;/a&gt;. You might be thinking what is the use of a POST request in which request's payload is a simple word called "PING"?, well, the answer is that you don't really need to send the data as a request payload.&lt;/p&gt;

&lt;h3&gt;
  
  
  You can use querystrings to send your data to a server
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a
    href="https://enmascript.com"
    ping="https://enmascript.com/api/tracking/?url=https://enmascript.com"&amp;gt;
&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way you would only need to parse the URL to get the data, in &lt;em&gt;NodeJS&lt;/em&gt; for example, you can use &lt;em&gt;Express&lt;/em&gt; and you will get access to the query string value through &lt;code&gt;req.query.url&lt;/code&gt;, which would return &lt;code&gt;https://enmascript.com&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  You can send cookies
&lt;/h3&gt;

&lt;p&gt;As with any post request, you can send information through your headers using cookies, here you can track the session and any data you may need regarding the recent action.&lt;/p&gt;

&lt;h3&gt;
  
  
  A real use case of the ping attribute: How Google does it
&lt;/h3&gt;

&lt;p&gt;The ping attribute is used by no less than Google, and not on any page, they use it to track users on their search result's page.&lt;/p&gt;

&lt;p&gt;If you go to google and make a search for "Enmascript" and inspect the first result, you will find this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W-38YlAw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2021-03-19-enmascript-google-ping.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W-38YlAw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2021-03-19-enmascript-google-ping.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And if you open your network and click on the link you will find a POST request with the PING payload executed to that URL:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PiRqoBdi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2021-03-19-enmascript-google-ping-request.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PiRqoBdi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2021-03-19-enmascript-google-ping-request.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I you look closely you can see that google is sending data through query strings and if you inspect the request headers, you will see that cookies are also being sent.&lt;/p&gt;

&lt;p&gt;Now, you might be thinking: &lt;strong&gt;this is great, but what about Firefox and browsers that do not support the attribute by default?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Glad you asked&lt;/strong&gt;, you can fallback to use a Beacon request or use a good old server redirect (both already explained above), in Google's case, they decided to handle this by using the redirect approach, so if you search in Firefox you will see that they are not using the &lt;code&gt;ping&lt;/code&gt; attribute anymore, instead, they change the anchor's &lt;code&gt;href&lt;/code&gt; attribute to be their redirect URL which finally leads to the destination URL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ejJjd84j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2021-03-19-enmascript-google-ping-firefox.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ejJjd84j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2021-03-19-enmascript-google-ping-firefox.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The goal of this article is to open your mind to other possibilities to improve the consistency of your application, specially on how to efficiently track data while giving users the best experience possible. Remember to always take into consideration your user's privacy and do not take this lightly, make sure to only track what is necessary and keep data anonymized.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed it!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Leenker.com a place to organize and share useful Links and bookmarks</title>
      <dc:creator>Enmanuel Durán</dc:creator>
      <pubDate>Fri, 29 Jan 2021 17:50:07 +0000</pubDate>
      <link>https://forem.com/duranenmanuel/leenker-com-a-place-to-organize-and-share-useful-links-and-bookmarks-45ld</link>
      <guid>https://forem.com/duranenmanuel/leenker-com-a-place-to-organize-and-share-useful-links-and-bookmarks-45ld</guid>
      <description>&lt;p&gt;Hey, this is Enmanuel, just another passionate software developer like you, I love innovating where possible and bringing ideas to life, my most recent creation being &lt;a href="https://leenker.com/"&gt;https://leenker.com/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In a nutshell, Leenker brings the Browser's bookmarks experience online with extra capabilities, it is probably my most ambitious project so far and I am very excited to share it, I thought about writing a long text about it, but it is better to let the project do the talking instead of getting you bored.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you want to get to know the why's of the platform, learn about it and its policy:&lt;/strong&gt; &lt;a href="https://leenker.com/help"&gt;https://leenker.com/help&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you want to use it, I would highly recommend you to do it alongside the chrome browser extension for a better Experience&lt;/strong&gt; &lt;a href="https://chrome.google.com/webstore/detail/leenker/dnkgochdfgehdfbkpkpdbccefphnnmoa"&gt;https://chrome.google.com/webstore/detail/leenker/dnkgochdfgehdfbkpkpdbccefphnnmoa&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, this is an in-progress project and has &lt;em&gt;many&lt;/em&gt; things to improve, (Accessibility, security...) take into consideration that it was built by only one person, so your feedback would be highly appreciated, any bugs or improvements you may recommend you can send them to &lt;a href="mailto:admin@leenker.com"&gt;admin@leenker.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope you like it.&lt;/p&gt;

&lt;p&gt;Come join me on &lt;a href="https://leenker.com/"&gt;https://leenker.com/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>node</category>
      <category>react</category>
      <category>projects</category>
    </item>
    <item>
      <title>Show and hide content on click with just HTML</title>
      <dc:creator>Enmanuel Durán</dc:creator>
      <pubDate>Mon, 14 Oct 2019 14:46:31 +0000</pubDate>
      <link>https://forem.com/duranenmanuel/show-and-hide-content-on-click-with-just-html-2ppl</link>
      <guid>https://forem.com/duranenmanuel/show-and-hide-content-on-click-with-just-html-2ppl</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted on &lt;a href="https://enmascript.com/articles/2019/10/14/show-and-hide-content-on-click-with-just-html"&gt;Enmascript.com&lt;/a&gt;, for a better reading experience go to the original post.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Multiple elements were introduced in HTML5 that are not yet widely known; one of them allows you to create interactive experiences such as accordions or dropdowns, the name of this element is &lt;code&gt;&amp;lt;details&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Toggling content visually with just HTML
&lt;/h2&gt;

&lt;p&gt;I recently wrote an article on &lt;a href="https://enmascript.com/articles/2019/09/26/toggle-content-on-click-without-javascript"&gt;how to create an onclick toggler without using javascript&lt;/a&gt; by leveraging the powerful CSS :target selector, but that is not the only/best way to achieve this.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;details&amp;gt;&lt;/code&gt; element allows us to implement a disclosure widget that hides and shows information depending on its boolean state &lt;code&gt;open&lt;/code&gt;. This allows us to toggle content interactively and natively:&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;details&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"details-example"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;If you click me I will display a list of content&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Option 1&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Option 2&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Option 3&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Demo&lt;/strong&gt;&lt;br&gt;
Available on &lt;a href="https://enmascript.com/articles/2019/10/14/show-and-hide-content-on-click-with-just-html"&gt;https://enmascript.com/articles/2019/10/14/show-and-hide-content-on-click-with-just-html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the &lt;code&gt;&amp;lt;summary&amp;gt;&lt;/code&gt; shows up, and when clicking it, we are showing the rest of the information (the list of options).&lt;/p&gt;

&lt;p&gt;We are able to achieve this with just HTML, I am adding some CSS to style the dropdown but only styles.&lt;/p&gt;
&lt;h2&gt;
  
  
  Extra tip (Javascript integration)
&lt;/h2&gt;

&lt;p&gt;You can also execute &lt;strong&gt;complementary javascript&lt;/strong&gt; when the open state of the details element changes, we can do that like this:&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;detailsElement&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;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.details-example&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;detailsElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;toggle&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&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;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;open&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;open&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="k"&gt;else&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;closed&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Very simple yet powerful feature, some of its more notorious applications are for accordions, dropdowns and toggling visual content in general. Companies such as github use it for their menus and interactions, hopefully now you will also be able to delete some javascript in your code base and use a native HTML element instead.&lt;/p&gt;

&lt;p&gt;If you want to read more about this element, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details"&gt;MDN has a pretty good explanation&lt;/a&gt; about the topic.&lt;/p&gt;

&lt;p&gt;Hope it was useful, see you guys in the next one.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>html</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Toggle content on click without javascript (Yes you read that right)</title>
      <dc:creator>Enmanuel Durán</dc:creator>
      <pubDate>Thu, 26 Sep 2019 14:42:56 +0000</pubDate>
      <link>https://forem.com/duranenmanuel/toggle-content-on-click-without-javascript-yes-you-read-that-right-4noh</link>
      <guid>https://forem.com/duranenmanuel/toggle-content-on-click-without-javascript-yes-you-read-that-right-4noh</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted on &lt;a href="https://enmascript.com/articles/2019/09/26/toggle-content-on-click-without-javascript"&gt;Enmascript.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Usually every time we want to toggle content on a website on click we use javascript. In this article, we are going to explore a different way of doing this by just using CSS capabilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  The power behind :target selector
&lt;/h2&gt;

&lt;p&gt;Not many people know this but there is a fantastic &lt;em&gt;pseudo-class&lt;/em&gt; that allows us to apply styles to elements when a specific hash link is present or not in an URL, the name of this &lt;em&gt;pseudo-element&lt;/em&gt; is &lt;code&gt;:target&lt;/code&gt; and it is the protagonist of this short article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Straight to the point, let's toggle stuff
&lt;/h2&gt;

&lt;p&gt;Based on the explanation above, you may now imagine what I am about to do, so let's do it:&lt;/p&gt;

&lt;p&gt;First, let's create a base HTML structure&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&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;This is some expandable content.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"hashLink"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        I have expanded like a Pufferfish.
        &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Please collapse this poor fish.&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#hashLink"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Expand&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and some CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nf"&gt;#hashLink&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;#hashLink&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;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&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;h2&gt;
  
  
  What are we doing here? let's explain
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;We are creating one anchor to add a hash &lt;code&gt;#hashLink&lt;/code&gt; to the URL (Expands the content).&lt;/li&gt;
&lt;li&gt;We are creating a second anchor inside of the second paragraph to remove that anchor and leave it empty (Collapses the content).&lt;/li&gt;
&lt;li&gt;The content we want to toggle is contained within a paragraph that has an id equal to the hash we are going to be adding to the URL.&lt;/li&gt;
&lt;li&gt;We are adding a &lt;code&gt;display: none;&lt;/code&gt; to the element with id &lt;code&gt;hashLink&lt;/code&gt; so that it is hidden by default.&lt;/li&gt;
&lt;li&gt;We are adding a &lt;code&gt;display: block;&lt;/code&gt; to the &lt;code&gt;#hashLink:target&lt;/code&gt; selector, this is what makes the magic happen, when the hash is present in the URL this style gets applied, creating the effect of toggling on click.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There is no really much more to explain, as you see it is straightforward, if you want to see a live example check the codepen below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/enmanuelduran/pen/LYPawYY"&gt;See this Pen from Enmanuel for a live Demo  LYPawYY&lt;/a&gt;&lt;br&gt;
  (&lt;a href="https://codepen.io/enmanuelduran"&gt;@enmanuelduran&lt;/a&gt;) on &lt;a href="https://codepen.io"&gt;CodePen&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This selector is powerful, be creative and feel free to post cool things you decide to do with it.&lt;/p&gt;

&lt;p&gt;Also you should know that there are other ways to accomplish this like using a hidden checkbox and toggle content on &lt;code&gt;checked&lt;/code&gt; state.&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>showdev</category>
    </item>
    <item>
      <title>6 Things you probably did not know javascript could do natively</title>
      <dc:creator>Enmanuel Durán</dc:creator>
      <pubDate>Tue, 24 Sep 2019 11:39:29 +0000</pubDate>
      <link>https://forem.com/duranenmanuel/6-things-you-probably-did-not-know-javascript-could-do-natively-2pen</link>
      <guid>https://forem.com/duranenmanuel/6-things-you-probably-did-not-know-javascript-could-do-natively-2pen</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted on &lt;a href="https://enmascript.com/articles/2019/09/24/6-things-you-probably-did-not-know-javascript-could-do-natively"&gt;Enmascript.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#Did you know&lt;/strong&gt; is a new series of Enmascript where we write short and concise explanations about topics we consider might be relevant or underestimated, if you are looking for more complete in-depth articles check out our &lt;strong&gt;&lt;a href="https://enmascript.com/series"&gt;other series&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;JavaScript is a language that evolves fast; for this reason, it is sometimes hard to catch up with all its features and capabilities. On this short article, we will explore some of the features that you may not know existed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting query string parameters
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;URLSearchParams&lt;/code&gt; is an interface that allows us to process query string parameters, it's been around for some years now but it might surprise you to know that it is not that popular between developers, let's see how to use it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// to get the query strings from the current url use window.location.search&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queryStrings&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;URLSearchParams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;?browser=chrome&amp;amp;action=redirect&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;queryStrings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;browser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;queryStrings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;action&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Simple enough, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams"&gt;here&lt;/a&gt; more information about the complete API, in case you are interested in the complete functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a unique list of elements using the Set object
&lt;/h2&gt;

&lt;p&gt;Creating unique lists in javascript is a common task, this is usually achieved by applying filters or for loops, but there is another way to accomplish this leveraging the &lt;code&gt;Set&lt;/code&gt; object.&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;list&lt;/span&gt; &lt;span class="o"&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&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;uniqueList&lt;/span&gt; &lt;span class="o"&gt;=&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;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We pass the array of primitive values to the &lt;code&gt;Set&lt;/code&gt; object; it creates a collection of unique values which is then converted to a &lt;em&gt;list&lt;/em&gt; using the spread operator syntax and array literals.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cast a list of primitive values to a different type
&lt;/h2&gt;

&lt;p&gt;Sometimes endpoints or processed data from the DOM doesn't return the type of values we need, I have seen this especially happening when processing dataset properties. Let's say we have the following list:&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;naiveList&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;1500&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;1350&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;4580&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We want to execute a sum of all the elements in the array, in JavaScript if you "sum" two strings like &lt;code&gt;'1' + '2'&lt;/code&gt; they will concatenate, generally, to solve this we would leverage the &lt;code&gt;parseInt&lt;/code&gt; function, but there is another way; We can cast the elements in the array to the type of primitive we need:&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;castedList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;naiveList&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;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;castedList&lt;/code&gt; now contains the values with the correct &lt;code&gt;Number&lt;/code&gt; type.&lt;/p&gt;

&lt;h2&gt;
  
  
  Flatten nested array values
&lt;/h2&gt;

&lt;p&gt;With the rise of Single Page Applications architectures such as Redux and concepts like frontend data normalization became more popular, part of this "data normalization" trend sometimes implies having all the ids of elements listed at the same level.&lt;/p&gt;

&lt;p&gt;Let's say we have the following list and we want to flatten it:&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;nestedList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;133&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;235&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;515&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;513&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&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;flattenList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nestedList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and just like that our array of ids is now flattened.&lt;/p&gt;

&lt;h2&gt;
  
  
  Avoid object mutations with Object.freeze
&lt;/h2&gt;

&lt;p&gt;When talking about functional code, one of the things that I have been asked the most is how to stop objects from mutating, the answer to this question is always the same &lt;em&gt;Freeze&lt;/em&gt; them:&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;immutableObject&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;Enmascript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://enmascript.com&lt;/span&gt;&lt;span class="dl"&gt;'&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="nx"&gt;freeze&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;immutableObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;immutableObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;twitter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://twitter.com/duranenmanuel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;immutableObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Another name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// immutableObject is still { name: "Enmascript", url: "https://enmascript.com" }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Created Controlled objects with Object.seal
&lt;/h2&gt;

&lt;p&gt;Does the same as &lt;code&gt;Object.freeze&lt;/code&gt; but it allows you to change the values of properties that were already defined in the object, this will enable you to _control _the properties declared in an object but not the definitions:&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;controlledObject&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;Barry Allen&lt;/span&gt;&lt;span class="dl"&gt;'&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="nx"&gt;seal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;controlledObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;controlledObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Clark Kent&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;controlledObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hero&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Superman&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// controlledObject will return { name: "Clark Kent" }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Do you know of any other javascript features that may not be popular but useful? &lt;a href="https://www.reddit.com/r/javascript/comments/d8njfy/6_things_you_probably_did_not_know_javascript/"&gt;share them with us&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>A different way of handling required arguments in javascript functions</title>
      <dc:creator>Enmanuel Durán</dc:creator>
      <pubDate>Sun, 22 Sep 2019 12:51:37 +0000</pubDate>
      <link>https://forem.com/duranenmanuel/a-different-way-of-handling-required-arguments-in-javascript-functions-2n57</link>
      <guid>https://forem.com/duranenmanuel/a-different-way-of-handling-required-arguments-in-javascript-functions-2n57</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted on &lt;a href="https://enmascript.com/articles/2019/09/22/a-different-way-of-handling-required-arguments-in-javascript-functions"&gt;Enmascript.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#Did you know&lt;/strong&gt; is a new series of Enmascript where we write short and concise explanations about topics we consider might be relevant or underestimated, if you are looking for more complete in-depth articles check out our &lt;strong&gt;&lt;a href="https://dev.to/series"&gt;other series&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Usually&lt;/em&gt; in javascript if you want to validate required arguments, you would do something similar to this:&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;function&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;water&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;food&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;candy&lt;/span&gt;&lt;span class="p"&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;water&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;food&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;water and food are required for Person&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="c1"&gt;// Do something with water and food...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The constructor &lt;code&gt;Person&lt;/code&gt; Above would throw an error if no &lt;code&gt;water&lt;/code&gt; or &lt;code&gt;food&lt;/code&gt; is supplied, this is a relatively common and popular way of validating parameters, but there is a different more functional way to achieve this.&lt;/p&gt;

&lt;p&gt;Create a helper function that throws an error:&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;required&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&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;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Parameter &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is required`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the case above we are passing an argument &lt;code&gt;name&lt;/code&gt; to the function because it feels more natural to print the name of the parameter that is required.&lt;/p&gt;

&lt;p&gt;Then we use it like this:&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;function&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;water&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;water&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;food&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;food&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;candy&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Do something with water and food&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What is happening here? How does this validation work? It is very straight forward:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we don't pass the required parameter's values, the &lt;code&gt;required&lt;/code&gt; function is executed throwing an error and forcing the user to pass each mandatory value everytime the function is invoked.&lt;/p&gt;

&lt;p&gt;Passing the name of the parameter to the &lt;code&gt;required&lt;/code&gt; function is only a preference; we can decide not to pass it and make the helper function simpler, it is a personal choice.&lt;/p&gt;

&lt;p&gt;This approach is not limited to this implementation, we can use this same principle to create our own argument's validators and reuse in our functions to enforce consistency and security.&lt;/p&gt;

&lt;p&gt;Do you have ideas that are similar or related to this topic? &lt;a href="https://www.reddit.com/r/javascript/comments/d7pvw7/a_different_way_of_handling_required_arguments_in/"&gt;share them with us&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Two exceptional use cases for the spread operator you may not know of</title>
      <dc:creator>Enmanuel Durán</dc:creator>
      <pubDate>Fri, 20 Sep 2019 13:44:37 +0000</pubDate>
      <link>https://forem.com/duranenmanuel/two-exceptional-use-cases-for-the-spread-operator-you-may-not-know-of-5ap8</link>
      <guid>https://forem.com/duranenmanuel/two-exceptional-use-cases-for-the-spread-operator-you-may-not-know-of-5ap8</guid>
      <description>&lt;p&gt;&lt;em&gt;originally posted on &lt;a href="https://enmascript.com/articles/2019/09/20/two-exceptional-use-cases-for-the-spread-operator-you-may-not-know-of"&gt;Enmascript.com&lt;/a&gt;&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#Did you know&lt;/strong&gt; is a new series of Enmascript where we write short and concise explanations about topics we consider might be relevant or underestimated, if you are looking for more complete in-depth articles check out our &lt;strong&gt;&lt;a href="https://dev.to/series"&gt;other series&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;One of the most popular features used in javascript after Ecmascript 2015 is without question the spread operator. In this short "Did you know" article, we will explore a couple of exceptional use cases that we consider to be very useful but that are not so well known.&lt;/p&gt;

&lt;h2&gt;
  
  
  Optional/Conditional Spreading
&lt;/h2&gt;

&lt;p&gt;Add object properties and values based on a condition:&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;isDog&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value&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="nx"&gt;isDog&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;woof&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this way, we can add the &lt;code&gt;woof&lt;/code&gt; property conditionally without having to use any if/else logic or similar approaches... &lt;strong&gt;if the condition returns true, it will add the property, otherwise, the object will remain untouched&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;result of &lt;code&gt;console.log(obj)&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;woof&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Copy objects and exclude selected properties
&lt;/h2&gt;

&lt;p&gt;Let's say we like the parts of certain CPU and we would like to keep most of them except for the ssd, because of course, we would like to have more space:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Original CPU&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;CPU&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;ram&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;32gb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;ssd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;64gb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;micro&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;i7&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// new CPU copy without the 64GB ssd&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ssd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;newCPU&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;CPU&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, if you console log &lt;code&gt;newCPU&lt;/code&gt; you will see that it does not longer contain the &lt;code&gt;ssd&lt;/code&gt; property, this happened because we excluded that property from the rest that were included in &lt;code&gt;newCPU&lt;/code&gt; by leveraging the &lt;em&gt;rest operator&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;result of &lt;code&gt;console.log(newCPU)&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;ram&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;16gb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;micro&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;i7&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Do you have any more relevant use cases for the spread operator you would like to comment about? go a head and do that on reddit or twitter in our social links.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Efficient behavioral tracking in javascript applications</title>
      <dc:creator>Enmanuel Durán</dc:creator>
      <pubDate>Mon, 15 Jul 2019 12:49:43 +0000</pubDate>
      <link>https://forem.com/duranenmanuel/efficient-behavioral-tracking-in-javascript-applications-g3e</link>
      <guid>https://forem.com/duranenmanuel/efficient-behavioral-tracking-in-javascript-applications-g3e</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://enmascript.com/articles/2019/07/15/efficient-behavioral-tracking-in-javascript-applications" rel="noopener noreferrer"&gt;Enmascript.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Tracking is an essential part of product development, either for the web, mobile applications, or any software you might be working on; it is crucial to understand your users to make your business grow. On this article, we are going to explore multiple alternatives and patterns to implement tracking using javascript.&lt;/p&gt;

&lt;p&gt;If you don't know what &lt;strong&gt;behavioral tracking&lt;/strong&gt; is or you have not implemented tracking in your projects, I can compress the explanation in a single paragraph:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Behavioral tracking&lt;/strong&gt; is the way companies get valuable information about meaningful events that have taken place in their platform/applications; this is especially useful to understand how users behave and to identify potential downfalls and opportunities in specific flows.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As you read in the simplistic definition above, it is all about getting valuable information from events, i.e., &lt;em&gt;call to action clicks, users logins...&lt;/em&gt; to achieve this as developers, we need a technical implementation that allows us to apply this in an efficient and scalable manner, but, as you will soon realize, tracking comes with some technical challenges.&lt;/p&gt;

&lt;h2&gt;
  
  
  A starting point
&lt;/h2&gt;

&lt;p&gt;Usually, you find that it is reasonably common between codebases to have isolated modules dedicated for tracking, these modules are just simple encapsulated functionalities that allow you to send information to an endpoint that stores the payload received from the users based on specific types of events.&lt;/p&gt;

&lt;p&gt;Below a naive implementation of how a tracking module could look:&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;class&lt;/span&gt; &lt;span class="nc"&gt;Tracker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nc"&gt;ENDPOINT_URL&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;my.endpoint.domain/tracking&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;track&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payload&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nx"&gt;Tracker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ENDPOINT_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                 &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                 &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&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;Content-Type&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;application/json&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="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payload&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;return&lt;/span&gt; &lt;span class="nx"&gt;response&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see above, we are just creating a class that contains a method that allows us to post information to an endpoint; this example is overly simple but is enough for this article's purpose, in a real-world escenario you will/should have a model that validates the parameters you want to track and the type of data sent as the payload.&lt;/p&gt;

&lt;p&gt;For this article's purpose, we are going to start by having as a target tracking a subscription button, this so that we can understand how many users are engaging with it.&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;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"js-tracked-click subscription-button"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    Subscription Button 1
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see how we can apply different patterns to track this same element.&lt;/p&gt;

&lt;h2&gt;
  
  
  In module tracking
&lt;/h2&gt;

&lt;p&gt;Consists of importing the tracking module in your application's modules and injecting the tracking function in the pieces of logic/relevant blocks of code. The implementation of this pattern would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Tracker from './Tracker';

class SubscriptionButton {
    constructor() {
        this._buttonHandler();
    }

    _onButtonClick() {
        console.log('Click handler function');

        Tracker.track({
            type: 'click',
            element: 'Subscription_button_1'
        });
    }

    _buttonHandler() {
        const button = document.querySelector('.js-tracked-click');

        button.addEventListener('click', this._onButtonClick.bind(this));
    }

    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Very simple and functional, this approach is widely used, it has some good and bad parts, lets analyze them:&lt;/p&gt;

&lt;h3&gt;
  
  
  Pros:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility&lt;/strong&gt;. Since we are adding the tracking method inside of the script's functionality, it is effortless to add tracking to pretty much any logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplicity&lt;/strong&gt;. Adding trackers is a simple task since it is just a matter of adding the function to the logic that requires it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unification&lt;/strong&gt;. The tracking code is in the same place as the original's script code, while this is bad on one side, it is good in the way that it allows you to be aware of it anytime you have to make a change on functionality.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Const:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Single responsibility is not respected&lt;/strong&gt;. Adding the tracking functionality inside of script's core code violates the single responsibility principle.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tracked elements are not easy to identify&lt;/strong&gt;. Each script contains the tracking functionality on its core which means that we need to go to its definition and look into the code where the tracking might be added&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability risk&lt;/strong&gt;: Since this approach is very flexible it can quickly get out of hand so it might be a good idea to establish some ground rules.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Isolating tracked methods by extending its original definition
&lt;/h2&gt;

&lt;p&gt;Extending the original class is another approach that seeks to isolate the elements that are tracked out of the original's script functionality, the idea is to extend the code to create an extra layer dedicated to tracking events, let's see an example:&lt;/p&gt;

&lt;p&gt;We implement the script functionality:&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;class&lt;/span&gt; &lt;span class="nc"&gt;SubscriptionButton&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&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="nf"&gt;_buttonHandler&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;_buttonHandler&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;_button&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;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.js-tracked-click&lt;/span&gt;&lt;span class="dl"&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;_button&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;click&lt;/span&gt;&lt;span class="dl"&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;onButtonClick&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="nf"&gt;_onButtonClick&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;elementHasClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentTarget&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;subscription-button&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;elementHasClass&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Click handler function&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="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;then we implement the tracking:&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;Tracker&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;./Tracker&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TrackedSubscriptionButton&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;SubscriptionButton&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&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="nf"&gt;_trackedMethods&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;_trackedMethods&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="nf"&gt;_onButtonClickTracking&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="nf"&gt;_anotherTrackedElement&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;_onButtonClickTracking&lt;/span&gt;&lt;span class="p"&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="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;elementHasClass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_button&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;click&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="nx"&gt;Tracker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;track&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Subscription_button_1&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="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;_anotherTrackedElement&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how we are able to isolate the tracking related code in a different class, it is essential that you realize that &lt;strong&gt;we have to be careful to not duplicate the logic for the element you want to track&lt;/strong&gt;, make sure the logic is trackable and reusable from the original class, notice that in the case above we are using a new event listener and condition, but the condition is actually the same from the parent's class, we are just reusing the property that defines it. This approach does not have to be implemented with inheritance; if you'd like to write functional and declarative code instead, you can use a &lt;a href="https://enmascript.com/articles/2019/02/27/higher-order-functions-and-some-great-applications-in-javascript" rel="noopener noreferrer"&gt;Higher Order Function&lt;/a&gt; that wraps the tracking functionality.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pros
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tracking code is isolated&lt;/strong&gt;. Single responsibility principle is respected.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tracked elements are natural to detect, modify, and delete&lt;/strong&gt;, this is simple to achieve since everything is a single place per each module.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;. If this approach is well applied, you can scale your codebase easily.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Const
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Flexible but with constraints.&lt;/strong&gt; We can add tracking to any element we want, but we always have to keep the tracking class into mind..&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mindset change&lt;/strong&gt;. When using this approach you need to always have tracking in your mind in the same way you do with unit testing, you always need to make sure your code is trackable in the isolated class, this can be good but has to be well thought.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dangerous code and duplicated logic&lt;/strong&gt;. If you notice the tracking class, you will see we are adding a specific listener to track the click event, this can be dangerous especially if there is logic you need to add around the tracking (like a conditional). Also, you will have to expose properties through &lt;code&gt;this&lt;/code&gt; so that the parent class can be inherited and used.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  A Custom approach
&lt;/h3&gt;

&lt;p&gt;Another way to keep tracking scalable and personalized is to create a customized centric tracking system, this pattern is prevalent and I have seen it being used in multiple companies, it usually consists on tracking interactions based on dataset properties, for example let's say you want to track a click on an element:&lt;/p&gt;

&lt;p&gt;Elements to track:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button data-click-tracking="subscription_button_left"&amp;gt;
    Subscribe
&amp;lt;/button&amp;gt;

&amp;lt;button data-click-tracking="subscription_button_right"&amp;gt;
    Subscribe
&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unified click tracker functionality:&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;Tracker&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;./Tracker&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ClickTracker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&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="nf"&gt;_bindClicks&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nc"&gt;TRACKED_ATTRIBUTE&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;data-click-tracking&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;static&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nc"&gt;TRACKED_ELEMENTS&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="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="s2"&gt;`[&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;ClickTracker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;TRACKED_ATTRIBUTE&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="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;_onClickHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentTarget&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="nx"&gt;ClickTracker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;TRACKED_ATTRIBUTE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="nx"&gt;Tracker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;track&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&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="nf"&gt;_bindClicks&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;ClickTracker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;TRACKED_ELEMENTS&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;element&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;element&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;click&lt;/span&gt;&lt;span class="dl"&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;_onClickHandler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way, all click tracked elements pass over the click handler and we are able to identify them by using a custom id passed through the dataset property. An excellent example of companies using this approach is Google on &lt;a href="https://tagmanager.google.com/" rel="noopener noreferrer"&gt;google tag manager&lt;/a&gt; where you can define custom classes or data properties to be tracked and send information to &lt;em&gt;Google Analytics&lt;/em&gt;. I consider this approach  to be the best of the ones mentioned so far since you can apply this same pattern for other types of events like scroll events, it is not limited to clicks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pros
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Custom implementation&lt;/strong&gt;. Made for the specific needs of the company.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability.&lt;/strong&gt; A single script is in charge of the tracking so the other scripts remain untouched.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Single Responsibility&lt;/strong&gt;, it is preserved because the tracking functionality is in a dedicated module.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Cons
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Constraints are present.&lt;/strong&gt; Since this approach consist on tracking elements from the DOM, it wont be possible to cover all the cases, you will find out that especial functionalities still need to be tracked on its core code,  this means that in especial ocassions you will have to import the tracking module and decide which approach you want to take In module tracking or extended approach.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Tracking asynchronous requests
&lt;/h2&gt;

&lt;p&gt;Generally, you find yourself needing to track a form submission or a login event, for many reasons is not efficient to add the tracking to the button that submits the information (The login could fail, or the form request could return an error) which means we would be tracking data incorrectly.&lt;/p&gt;

&lt;p&gt;For this, you could use the In module tracking approach by adding the tracking function to the &lt;code&gt;200&lt;/code&gt; response, this would be fine but we would endup with multiple conditions for each request needed to be tracked.&lt;/p&gt;

&lt;p&gt;let's say you have a centralized HTTP client that you use for all asynchronous request (which will almost always be the case); this client returns a promise so that you can execute some code per module, then we get assigned some tracking requirements as follow:&lt;/p&gt;

&lt;p&gt;We would like to track the following events to get some meaningful information about our users and to learn how we can improve their experience on the platform:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Successful login events&lt;/li&gt;
&lt;li&gt;Successful Subscription events&lt;/li&gt;
&lt;li&gt;Logout events&lt;/li&gt;
&lt;li&gt;Call to action clicks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So we notice that the call to action click can be easily tracked with a click tracking event, but what about the other ones? All of them are different events using different URLs and needing different data to be tracked, so if we use a centralize HTTP client it would look something like 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;function&lt;/span&gt; &lt;span class="nf"&gt;HTTPPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&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;return&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&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;Content-Type&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;application/json&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="na"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;no-cache&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;follow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;referrer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;no-referrer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;HTTPPost&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then we would be able to use it to track data like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import HTTPPost from './http-client';

HTTPPost('/api/login', {userId, password, source: 'modal' })
    .then(response =&amp;gt; {
        Tracker.track({ type: 'successful-login', ...response })
    }
    .catch(error =&amp;gt; console.error(error))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The approach above is not actually bad but we would have to import the Tracker module in every file that will execute the successful asynchronous request which sometimes is something that will be a let down depending on the company's policies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Centralizing Asynchronous tracking
&lt;/h2&gt;

&lt;p&gt;This will be the last approach we will cover on this article and it is one that I really like. The fundaments of this approach relies on adding the tracking function once in the &lt;code&gt;HTTPPost&lt;/code&gt; method, then we can leverage a dictionary that will contain the URLs we want to track, these will be mapped to a model of properties where each URL will require to be successfully tracked, something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2019-07-15-behavioral-tracking-async.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fenmascript.com%2Fimages%2F2019-07-15-behavioral-tracking-async.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's explain with code step by step:&lt;/p&gt;

&lt;h3&gt;
  
  
  1) We add the tracking in the HTTPClient
&lt;/h3&gt;

&lt;p&gt;We basically take the code from the previous approach and add the tracking on the promise response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Tracker from './Tracker';

function HTTPPost(url = '', data = {}) {
    return fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
         },
        cache: 'no-cache',
        redirect: 'follow',
        referrer: 'no-referrer',
        body: JSON.stringify(data),
    })
    .then(response =&amp;gt; response.json())
    .then(response =&amp;gt; Tracker.request(url, response));
}

export default HTTPPost;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see we are executing &lt;code&gt;Tracker.request&lt;/code&gt; on all requests, now we have to define which requests we actually want to track and which parameters are relevant to be tracked for those requests, so we can make use of a dictionary like 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;TRACKED_URLS&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;/api/login&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;userId&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;source&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;url&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;type&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;/api/logout&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;userId&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;time&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;type&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;api/subscription&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;userId&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;source&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;type&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="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;TRACKED_URLS&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 example above we are using a list to store the valid properties just to make the example simpler, you can create a real model that properly validates the information that each tracked URL needs. After this, the method in charge of tracking the requests could be added to the tracking module. We can do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import TRACKED_URLS from './tracked-urls';

class Tracker {
    static get ENDPOINT_URL() {
        return "my.endpoint.domain/tracking"
    }

    async track(payload) {
        const response = await fetch(
            Tracker.ENDPOINT_URL,
            {
                 method: 'POST',
                 headers: {
                     'Content-Type': 'application/json',
                 },
                 body: JSON.stringify(payload)
            }
        );

        return response;
    }

    request(url, data) {
        const URL_PROPERTIES = TRACKED_URLS[url];
        const PAYLOAD_PROPERTIES = Object.keys(data);

        const arePropertiesValid = URL_PROPERTIES
            &amp;amp;&amp;amp; URL_PROPERTIES.every(property =&amp;gt; (
                PAYLOAD_PROPERTIES.includes(property)
            ));

        if (!arePropertiesValid) return false;

        this.track(data);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Very simple, the &lt;code&gt;request&lt;/code&gt; method just verifies that all the tracked elements have the correct properties passed, it serves as a centralized filter and as a centralized request's tracking dictionary, This approach is straight forward and scales very well because you have all the tracked URL's in a single place which allows you to add and delete on demand quickly.&lt;/p&gt;

&lt;p&gt;As stated at the beginning, this article's intention is to show the good and the bad parts of each tracking implementation so that you can decide which one is better for you and your team.&lt;/p&gt;

&lt;p&gt;That is it for now, I hope you have enjoyed it -- if you did, remember that you can share it with your friends or leave a comment on reddit or twitter by clicking on the social links.&lt;/p&gt;

&lt;p&gt;See you in the next one!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Code quality and web performance, the myths, the do's and the don'ts</title>
      <dc:creator>Enmanuel Durán</dc:creator>
      <pubDate>Tue, 04 Jun 2019 14:38:56 +0000</pubDate>
      <link>https://forem.com/duranenmanuel/code-quality-and-web-performance-the-myths-the-do-s-and-the-don-ts-4kfg</link>
      <guid>https://forem.com/duranenmanuel/code-quality-and-web-performance-the-myths-the-do-s-and-the-don-ts-4kfg</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally posted on &lt;a href="https://enmascript.com/articles/2019/06/04/code-quality-and-web-performance-the-myths-the-dos-and-the-donts"&gt;Enmascript.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Optimization is a word that englobes a significant scope of possibilities, especially in the world of software development. Developers always like to "optimize," but what is it &lt;em&gt;really&lt;/em&gt;? -- This word is many times miss-used to excuse subjective point of views, other times it is used only to demonstrate "improvements" that are not sincerely necessary. In this article, we are going to be talking about the mistakes we make as developers when trying to improve our code bases by "optimizing" them, and, we are going to see which are some excellent ways to start doing this correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Always measure first, quantify, and please... don't just blindly "optimize".
&lt;/h2&gt;

&lt;p&gt;A fundamental lesson that we as software developer learn with the years is that &lt;strong&gt;numbers matter&lt;/strong&gt;, &lt;em&gt;something is impactful if you have a way of quantifying the co-relation between the effort you have applied to it and the results it has given&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For example, there is a common belief that lines of code are somehow inversely related to higher performance or better quality.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tcD6WDPg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2019-06-03-common-belief.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tcD6WDPg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2019-06-03-common-belief.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Usually&lt;/em&gt; having less code means less surface area to cover, which means, more clarity and less possibility of bugs. However, this only applies if you have a "high quality" code base that is secure, consistent and is using the right tools for the right job.&lt;/p&gt;

&lt;p&gt;Let me elaborate a little bit, take the following piece of code:&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;mainWrapper&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;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'.wrapper'&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;mainWrapper&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;element&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;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'.inner-box'&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;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;enabledInteractionClass&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;mainWrapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;enabledInteractionClass&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;There is nothing wrong with the code above. It just has an extra couple of variables to make the code more declarative. I have seen code reviews where people convert this implementation to:&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;mainWrapper&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;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'.wrapper'&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;enabledInteractionClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'enabled-box'&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;enabledElementClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mainWrapper&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;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'.inner-elm'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;enabledInteractionClass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;mainWrapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;enabledInteractionClass&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then, the argument comes where a developer says, "we can avoid the extra variable for &lt;code&gt;document.querySelector('.inner-elm')&lt;/code&gt;" and he/she calls this an optimization, your answer is that you like it more the first way because it is more declarative in comparison, and you spent 5 mins discussing this. What the code reviewer does not realize is that he is now defining &lt;code&gt;enabledInteractionClass&lt;/code&gt; all the time, and he is using the variable &lt;code&gt;enabledElementClass&lt;/code&gt; for something that does not require it, so where is the improvement? Was this a decision based on a solid foundation? How does this code make the codebase better? is it measurable?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt;: I'm in favor of well constructed/used ternary operators and &lt;a href="https://enmascript.com/articles/2018/07/09/short-circuiting-for-faster-evaluations"&gt;short-circuiting&lt;/a&gt;, this example just shows how &lt;em&gt;NOT&lt;/em&gt; to use a tool just for the sake of less lines of code or to defend a subjective point of view.&lt;/p&gt;

&lt;h2&gt;
  
  
  There is a term for worthless micro-optimizations that end up in a waste of time.
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Bikeshedding&lt;/em&gt; is the art of wasting time by having technical disputes over minor, marginal issues, issues that won't have a real positive impact in your work while there are more important things to look at. So, the next time someone wants to argue over insignificant improvements, just say the word &lt;em&gt;bikeshedding&lt;/em&gt;. The etymology for this term can be found on &lt;a href="https://en.wiktionary.org/wiki/bikeshedding#English"&gt;wiktionary.org&lt;/a&gt; and &lt;a href="https://www.urbandictionary.com/define.php?term=bikeshedding"&gt;urbandictionary has another good definition for it&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The point being, micro-optimizations are something you should avoid doing without basis, if you want to optimize some code, measure and identify bottlenecks first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measure first
&lt;/h2&gt;

&lt;p&gt;Suppose your website/application is a little bit slow when you scroll, don't just go and increase the &lt;code&gt;throttle&lt;/code&gt; time for your listener and mark it as &lt;em&gt;done&lt;/em&gt;, analyze what is going on and understand what the reason behind this issue is. A way to measure scroll performance is through &lt;em&gt;Frames Per Second (FPS)&lt;/em&gt;, so you know you have a quantifiable way to understand what is happening and a starting point.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's see a good way to tackle this problem
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;First, check if the code/tasks executed on the scroll are doing what they suppose to be doing, scroll junkiness could be caused by memory leaks.&lt;/li&gt;
&lt;li&gt;If the code is wrong, fix it, if it is ok look at your alternatives:

&lt;ul&gt;
&lt;li&gt;Can we use &lt;code&gt;IntersectionObserver&lt;/code&gt; instead of the scroll event?&lt;/li&gt;
&lt;li&gt;Is &lt;code&gt;throttle&lt;/code&gt; needed? can we &lt;code&gt;debounce&lt;/code&gt; it?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If after looking at your alternatives you don't see any other option then as a last resource &lt;em&gt;consider&lt;/em&gt; increasing the &lt;code&gt;throttle&lt;/code&gt; time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Why to go through all this trouble instead of just increasing the time? because nothing ensures you that after increasing the time it won't happen again, or even worst, nothing ensures that there is not a more important issue underneath.&lt;/p&gt;

&lt;p&gt;This is just an example applied to scroll performance, but the same principle can be applied over any other circumstance by having the correct metric to measure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Improve the quality of your code base
&lt;/h2&gt;

&lt;p&gt;Some of the most common ways of optimizing code bases are linters, if you use javascript &lt;a href="https://eslint.org/"&gt;ESLint&lt;/a&gt; is a good one -- As for your styles, you could use &lt;a href="https://stylelint.io/"&gt;Stylelint&lt;/a&gt;. You can later add these two elements as required steps in your build process to create an extra layer of consistency, linters are a good way to promote consistency in your code base.&lt;/p&gt;

&lt;p&gt;Nevertheless, linters alone are not going to guarantee the quality of your code base, it heavily depends on the level of your team, you have to be aware of this before trying to push things too hard. Some recommendations to start increasing the quality of your codebase are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Promote design patterns&lt;/strong&gt;. (We have written about some of them, i.e &lt;a href="https://enmascript.com/articles/2019/03/09/diving-into-the-great-observer-pattern-in-javascript"&gt;Observer Pattern&lt;/a&gt;, &lt;a href="https://enmascript.com/articles/2018/10/05/javascript-factory-pattern"&gt;Factory Pattern&lt;/a&gt;, &lt;a href="https://enmascript.com/articles/2019/03/18/building-objects-progressively-with-the-builder-pattern-in-javascript"&gt;Builder&lt;/a&gt;) you don't have to use these explicitly, remember, the idea is to use the right tool for the right job, design patterns are not always the best option, but they are for sure a way of adding consistency and security to your application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pairing Sessions / Brainstorming.&lt;/strong&gt; Some hugely underestimated and yet powerful tools are communication and teamwork, make sure the team communicates their ideas before implementing them at least with one other person, this ensures that the knowledge spreads, more people gets involved and gives their opinion. Together you will find a more solid approach to solve a problem and you will avoid the fatigue of rejected pull requests, a couple of good ways to promote team communication are &lt;a href="https://en.wikipedia.org/wiki/Kata_(programming)"&gt;Katas&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Request_for_Comments"&gt;RFC's&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Your future self matters&lt;/strong&gt;. When you write code or make a code review, think in your future self, what would happen tomorrow if you had to scale your module? I like to follow the &lt;em&gt;DIRTFT (Do It Right The First Time)&lt;/em&gt; principle, this is certainly not easy to do, even today I don't get to write the best possible code I can when I do it the first time, but I iterate over it until I get to a place where I feel comfortable with it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Think before you code&lt;/strong&gt;. Create a model about what you have to do, understand what is needed and the alternatives you have, never start writing code right away before analyzing the implementation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Care about data structures and time/space complexity&lt;/strong&gt;. This point requires deeper knowledge on &lt;strong&gt;how&lt;/strong&gt; algorithms work and &lt;strong&gt;why&lt;/strong&gt;. I have personally noticed that the more experienced the team is the more they care about this, in the frontend world there is a miss-conception that data structures/time space complexity does not matter as much, but this is a mistake, these tools allow you to write scalable/performant modules, it is part of choosing the right tool for the right job.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There is not a fixed formula for &lt;em&gt;code quality&lt;/em&gt;, this is a progressive and subjective topic, it highly depends on the requirements of the company you are working on and your team. One last recommendation I could give you is to &lt;strong&gt;cosider promoting declarative code&lt;/strong&gt;, give attention to the readability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recommendations, tips and good practices to optimize your site (Performance):
&lt;/h2&gt;

&lt;p&gt;One excellent way to measure your site's performance is through the &lt;a href="https://developers.google.com/web/fundamentals/performance/rail"&gt;RAIL model&lt;/a&gt;. Below I have a list of some common problems that websites confront and some possible tools that could help you solve them, this list is not meant to solve your problems directly but to point you in the right direction:&lt;/p&gt;

&lt;h4&gt;
  
  
  Unused code
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Impact metric&lt;/strong&gt;: Bytes / Download time&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Helpful tools&lt;/strong&gt;: &lt;a href="https://www.npmjs.com/package/webpack-bundle-analyzer"&gt;webpack-bundle-analyzer&lt;/a&gt;, &lt;a href="https://developers.google.com/web/updates/2017/04/devtools-release-notes#coverage"&gt;Chrome Coverage&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;possible solutions&lt;/strong&gt;: &lt;a href="https://webpack.js.org/guides/tree-shaking/"&gt;Tree Shaking&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Scroll Junk
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Impact metric&lt;/strong&gt;: Frames Per Second (FPS) / Page Interaction&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Helpful tools&lt;/strong&gt;: &lt;a href="https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/reference#fps-meter"&gt;Chrome's FPS meter&lt;/a&gt;, &lt;a href="https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/reference#fps-chart"&gt;Chrome's FPS Chart&lt;/a&gt;, &lt;a href="https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/reference#frames"&gt;Chrome's Frames Section&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;possible solutions&lt;/strong&gt;: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API"&gt;Intersection Observer&lt;/a&gt;, &lt;a href="https://enmascript.com/articles/2019/02/27/higher-order-functions-and-some-great-applications-in-javascript#throttler"&gt;Throttling&lt;/a&gt;, &lt;a href="https://lodash.com/docs/4.17.11#debounce"&gt;Debouncing&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Heavy/Slow Animations
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Impact metric&lt;/strong&gt;: Frames Per Second (FPS) / UI / UX&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Helpful tools&lt;/strong&gt;: Same as Scroll Junk
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;possible solutions&lt;/strong&gt;: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/will-change"&gt;CSS will-change property&lt;/a&gt;, &lt;a href="https://developers.google.com/web/fundamentals/performance/rendering/stick-to-compositor-only-properties-and-manage-layer-count"&gt;Use properties that are processed by the GPU&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Elevated Number of Network Requests
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Impact metric&lt;/strong&gt;: Seconds/Milliseconds &lt;em&gt;Load Time&lt;/em&gt; / Application Response Time&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Helpful tools&lt;/strong&gt;: &lt;a href="https://enmascript.com/articles/2019/04/22/avoid-recomputing-heavy-tasks-by-leveraging-memoization-in-javascript#a-better-implementation"&gt;Memoizer&lt;/a&gt;, &lt;a href="https://redis.io/"&gt;Redis&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;possible solutions&lt;/strong&gt;: &lt;a href="https://enmascript.com/articles/2019/04/22/avoid-recomputing-heavy-tasks-by-leveraging-memoization-in-javascript"&gt;Memoization&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/Cache_(computing)"&gt;Caching &lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Memory Leaks
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Impact metric&lt;/strong&gt;: Bytes / Browsers Memory&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Helpful tools&lt;/strong&gt;: &lt;a href="https://developers.google.com/web/tools/chrome-devtools/memory-problems/heap-snapshots"&gt;Chrome's Heap Snapshot&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;possible solutions&lt;/strong&gt;: &lt;a href="https://www.w3schools.com/js/js_function_closures.asp"&gt;Closures&lt;/a&gt;, &lt;a href="https://developers.google.com/web/tools/chrome-devtools/memory-problems/#discover_detached_dom_tree_memory_leaks_with_heap_snapshots"&gt;Remove dettached DOM nodes&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Extra tools
&lt;/h4&gt;

&lt;p&gt;There are many other metrics and important optimizations you can make, it is impossible to fit them all in a list but the tools below are a big help as far as web optimization is concerned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developers.google.com/web/tools/lighthouse/"&gt;LightHouse&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.webpagetest.org/"&gt;webpagetest.org&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  This is just a starting point
&lt;/h2&gt;

&lt;p&gt;The path to web performance and code quality is long, it is not straightforward in most of the cases, there are some things that you only learn from experience. This article is meant to be a starting point for those who are looking for directions but don't know where to start or how to start.&lt;/p&gt;

&lt;p&gt;Hope you have enjoyed this article, see you in the next one!&lt;/p&gt;

</description>
      <category>performance</category>
      <category>javascript</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Avoid recomputing heavy tasks by leveraging memoization in javascript</title>
      <dc:creator>Enmanuel Durán</dc:creator>
      <pubDate>Mon, 22 Apr 2019 13:09:22 +0000</pubDate>
      <link>https://forem.com/duranenmanuel/avoid-recomputing-heavy-tasks-by-leveraging-memoization-in-javascript-4bad</link>
      <guid>https://forem.com/duranenmanuel/avoid-recomputing-heavy-tasks-by-leveraging-memoization-in-javascript-4bad</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted in &lt;a href="https://enmascript.com/articles/2019/04/22/avoid-recomputing-heavy-tasks-by-leveraging-memoization-in-javascript"&gt;Enmascript&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Javascript is an &lt;em&gt;wonderful&lt;/em&gt; dynamic language; it allows us to do many things either on the backend layer with Node or the frontend of our application. One of the things that we do the most in javascript is endpoint calls that are later used to compute information and finally show it to the user in a friendly and polished way. We usually try to avoid at all cost additional requests and computations so that our application performs at its best. On this article, we're going to be exploring a technique that will help us improve on this matter, so without further ado let's get into it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The premise
&lt;/h2&gt;

&lt;p&gt;The idea is to take advantage of the browser's memory to store data that usually requires heavy computations to obtain it, and that's likely to be reused in the future, in this way we have the information ready to be accessed, and we don't have to re-run tasks to get it, this technique is known as &lt;em&gt;memoization&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the dynamic behind it
&lt;/h2&gt;

&lt;p&gt;The image below explains the basic flow behind memoization to understand better how it fits in our application:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's analyze this image:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ehQYpatP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2019-04-20-memoizing-explained.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ehQYpatP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2019-04-20-memoizing-explained.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We're trying to call a memoized function with arguments A and B. Before executing it the memoization layer checks if it was called in the past with the same arguments, but, since it's not the case, it passes the information over and executes it on the first request (Request #1). The function then makes the computations and returns the result to the memoization layer where it's stored for later use. Now, the next time we try to run the function with the same parameters, the memoization layer will check if the arguments are recurrent, and, since they'll be we'll get the information from memory (Request #2) and then return it to us directly.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to apply
&lt;/h2&gt;

&lt;p&gt;It's recommended that we employ this technique when our function meets the following requirements:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Performs heavy tasks, therefore storing the result might help us save some processing time in our application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Consistently receives the same arguments, so that we can avoid re-computing repeated data.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What is required to memoize our functions?
&lt;/h2&gt;

&lt;p&gt;Ideally, to memoize a function it should follow the principles of &lt;strong&gt;purity&lt;/strong&gt;, meaning that it should not contain side effects, it should explicitly depend on its arguments and return the same output for the same arguments passed. In this way, we can guarantee a successful memoization layer that's automatic and argument-dependent; &lt;em&gt;this is pivotal for the technique to work consistently&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A use case
&lt;/h2&gt;

&lt;p&gt;As mentioned at the beginning, one good use case for memoization is to avoid making extra network requests when the data we need has been already fetched and the computations to clean and show these data were already executed.&lt;/p&gt;

&lt;p&gt;If you have what we call a "robust" architecture, you probably have a caching layer in the backend like Redis to avoid receiving consecutive hits on the database every time a user asks for recurrent information. If we apply this technique to the function that executes the GET request, we can even avoid making the endpoint call meaning that the network won't get agglomerated with tasks. Let's see how we can interpret this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aYwq3CBA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2019-04-20-memoizing-with-requests.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aYwq3CBA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://enmascript.com/images/2019-04-20-memoizing-with-requests.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, this being said, memoization is something you'll apply in your applications as you notice that your functions &lt;a href="http://localhost:8000/articles/2019/04/19/avoid-recomputing-heavy-tasks-by-leveraging-memoization-in-javascript#when-to-apply"&gt;meet the requirements&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing a simple memoizer
&lt;/h2&gt;

&lt;p&gt;Ok, it's been enough drawing and theory, let's create our own custom memoizer.&lt;/p&gt;

&lt;p&gt;For what we've learned we know that the &lt;em&gt;memoizer&lt;/em&gt; should receive a function as argument, then, we need to store the results in some data structure to be able to retrieve them later based on the function and arguments passed, let's see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Receives a function which would be memoized
 */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;memoize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* we'll use this object to store the results */&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * Returns a function that will receive the arguments
     * that will be passed to the memoized function (fn).
     */&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&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;/* We stringify the arguments in case they're non-primitive values */&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cacheKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="cm"&gt;/* were the arguments already passed? if no, then store the result */&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;cacheKey&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;cacheKey&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="cm"&gt;/* We then return the stored result */&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;cacheKey&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;And then we would use it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* A simple add function that will be memoized */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Executing the sum&lt;/span&gt;&lt;span class="dl"&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* we pass the function definition to the memoizer */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;memoizedAdd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;memoize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/* we now run the memoized add function */&lt;/span&gt;
&lt;span class="nx"&gt;memoizedAdd&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs "Executing the sum" and returns 3&lt;/span&gt;
&lt;span class="nx"&gt;memoizedAdd&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// doesn't log anything, returns 3 directly.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the implementation of the &lt;code&gt;memoize&lt;/code&gt; function persistency works thanks to closures, The &lt;code&gt;cache&lt;/code&gt; object is stored in memory thanks to the variable &lt;code&gt;memoizedAdd&lt;/code&gt;, then, thanks to that variable we're able to execute the returned function and get the results as needed. If you have issues trying to understand this concept, I recommend you to read the article about &lt;a href="https://enmascript.com/articles/2019/02/27/higher-order-functions-and-some-great-applications-in-javascript"&gt;Higher Order Functions&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A better implementation
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;If you analyze the previous implementation a little bit you will realize that we have to worry about a couple of things:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memory consumption&lt;/strong&gt;, if the results and keys we're storing are large sets of data and we store them multiple times this &lt;em&gt;could&lt;/em&gt; cause memory issues in the future, this won't happen often but it's important to keep it in mind.&lt;/p&gt;

&lt;p&gt;Knowing this, here a more &lt;em&gt;complete&lt;/em&gt; implementation that will allow you to clear the cached elements.&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;function&lt;/span&gt; &lt;span class="nx"&gt;memoize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&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;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;cache&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="kd"&gt;function&lt;/span&gt; &lt;span class="kd"&gt;get&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="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;freeze&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="nx"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;count&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="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="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;process&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;cacheKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arguments&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;cacheKey&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;cache&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;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;cacheKey&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;cacheKey&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&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;arguments&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;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;cacheKey&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="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;freeze&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kd"&gt;get&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;With this we're able to clear the cache manually when we need it, and example of usage:&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;memoizedAdd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;memoize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;memoizedAdd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;process&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Executes the function&lt;/span&gt;
&lt;span class="nx"&gt;memoizedAdd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;process&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Hits the cache&lt;/span&gt;

&lt;span class="nx"&gt;memoizedAdd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// returns the object with the memoized elements&lt;/span&gt;
&lt;span class="nx"&gt;memoizedAdd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// returns the number of elements stored so far&lt;/span&gt;
&lt;span class="nx"&gt;memoizedAdd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// clears the cache&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And just like that we have a more complete implementation. Also, thanks to the &lt;code&gt;count&lt;/code&gt; and &lt;code&gt;clear&lt;/code&gt; method we can clear the cache when we consider it relevant.&lt;/p&gt;

&lt;p&gt;It's worth mentioning that this technique is widely used, if you have used selectors in redux in the past you'll find out that  &lt;a href="https://github.com/reduxjs/reselect"&gt;Reselect&lt;/a&gt; applies memoization; also libraries like &lt;code&gt;lodash&lt;/code&gt; have their own implementation...&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important to know:&lt;/strong&gt; you don't explicitly need a function called &lt;em&gt;memoize&lt;/em&gt; or an isolated function for that matter to apply this technique, as long as your implementation respects the principle behind memoization, you can implement it in a custom way based on the functions you're working on, the examples on this article are just a reusable way to apply this principle.&lt;/p&gt;

&lt;p&gt;Ok, guys, I think that's it for  now, I hope you've enjoyed the article... If it was the case remember to share it with your friends and coworkers. If any questions remember you can catch me in twitter at &lt;a href="https://twitter.com/duranenmanuel"&gt;@duranenmanuel&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;See you in the next one!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>performance</category>
      <category>memoization</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
