<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Diogo Almeida</title>
    <description>The latest articles on DEV Community by Diogo Almeida (@red-dial).</description>
    <link>https://dev.to/red-dial</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%2F1407867%2F8011a728-e428-4e01-b6d0-85087e0d2bd3.jpg</url>
      <title>DEV Community: Diogo Almeida</title>
      <link>https://dev.to/red-dial</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/red-dial"/>
    <language>en</language>
    <item>
      <title>Improving Coupling in your Project</title>
      <dc:creator>Diogo Almeida</dc:creator>
      <pubDate>Mon, 23 Dec 2024 14:29:59 +0000</pubDate>
      <link>https://dev.to/red-dial/improving-coupling-in-your-project-2hg4</link>
      <guid>https://dev.to/red-dial/improving-coupling-in-your-project-2hg4</guid>
      <description>&lt;p&gt;Hey everyone! This post is meant to help you improve how your project's data types are connected, making them more reliable and easier to understand.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TLDR&lt;/strong&gt; for you folks in a hurry: avoid repeating &lt;code&gt;types&lt;/code&gt; and &lt;code&gt;consts&lt;/code&gt; that refer to the same thing and try to give them suggestive names (even if they are a simple &lt;code&gt;string&lt;/code&gt; or a &lt;code&gt;number&lt;/code&gt;). This will make it easier for you to identify, modify, or remove your code later.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Imagine you are developing an e-commerce website, and you have defined the product's &lt;code&gt;type&lt;/code&gt; as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ProductType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&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;On a project of this kind, you can easily find multiple ways where one might refer to a product's id, from simple functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;const getProductById = (products: ProductType[], id: string) =&amp;gt; {...};&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;const onProductPress = (productId: string) =&amp;gt; {...};&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To more advanced situations, such as saving a product's data in a state store, or passing props to other components in JS Frameworks&lt;/p&gt;




&lt;h2&gt;
  
  
  The Issues
&lt;/h2&gt;

&lt;p&gt;Now let's address the issue(s) with this approach:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What if the product's id wasn't this simple and easy to remember? What if it was something like &lt;code&gt;ab12-w35-s48-09&lt;/code&gt; (representing for example &lt;code&gt;vendor-category-product-variant&lt;/code&gt;)? &lt;/li&gt;
&lt;li&gt;What if, and this is my key point, you had to change the &lt;code&gt;type&lt;/code&gt; of the product's id, in all of its occurrences throughout your project?
This could be particularly difficult if you gave it different names (like &lt;code&gt;productId&lt;/code&gt;, &lt;code&gt;product_id&lt;/code&gt;, &lt;code&gt;pid&lt;/code&gt;, or &lt;code&gt;id&lt;/code&gt;) when referring to it in different files. You couldn't just search for &lt;code&gt;string&lt;/code&gt; either because you would most certainly find many occurrences that were not related to it.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  The Potential Solutions
&lt;/h2&gt;

&lt;p&gt;To address issue one, you could use &lt;a href="https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html" rel="noopener noreferrer"&gt;&lt;em&gt;Template Literals Types&lt;/em&gt;&lt;/a&gt;, which would make the new product id's &lt;code&gt;type&lt;/code&gt;: &lt;code&gt;${string}-${string}-${string}-${string}&lt;/code&gt;. Repeating this across multiple files would now be annoying, so one could either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create a custom &lt;code&gt;type&lt;/code&gt; for the &lt;code&gt;id&lt;/code&gt; field, and use it in the &lt;code&gt;ProductType&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ProductIdType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="kr"&gt;string&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="kr"&gt;string&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="kr"&gt;string&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="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If needed, you could also create and refer to different types for each string, or refer to others you created previously. Finally you would use the new type as such:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ProductIdType&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;or you could refer to the &lt;code&gt;id&lt;/code&gt; entry of the &lt;code&gt;ProductType&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ProductType&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{...}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both these approaches would solve issue two: wherever you'd find &lt;code&gt;ProductIdType&lt;/code&gt; or &lt;code&gt;ProductType['id']&lt;/code&gt;, you'd know that you were dealing with a product's id, and knew that you should replace it.&lt;/p&gt;

&lt;p&gt;The first solution might seem friendlier, but you would now have a bi-parted structure, where you have one &lt;code&gt;type&lt;/code&gt; for the product and another for the id, which can be used independently. Here's an example representation of said structure:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqofidt6pzub5kx7tuosg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqofidt6pzub5kx7tuosg.png" alt="Bi-parted structure" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is no doubt a smaller issue, but if you change/delete the &lt;code&gt;id&lt;/code&gt; entry of the &lt;code&gt;ProductType&lt;/code&gt;, that change wouldn't be reflected in your whole project.&lt;/p&gt;

&lt;p&gt;The last approach, however, is the one I usually follow, because it increases your data's &lt;em&gt;coupling&lt;/em&gt; (by lack of a better word). All your references to the product's data now point directly to the &lt;code&gt;ProductType&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa4ti13nsa8edqcor9frc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa4ti13nsa8edqcor9frc.png" alt="Single structure" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;




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

&lt;blockquote&gt;
&lt;p&gt;I'm not saying you should always create &lt;code&gt;types&lt;/code&gt; for all your data. Whenever I see myself repeating references to the same data's &lt;code&gt;type&lt;/code&gt;, I usually opt to access the original one, as in the second approach.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;u&gt;Bonus tip 1:&lt;/u&gt; You can apply the same logic for &lt;code&gt;consts&lt;/code&gt;: If you see yourself repeating the same magic number or string in multiple places, it's best to give it a proper designation and use it.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Bonus tip 2:&lt;/u&gt; Use Pick, Omit and Exclude and other &lt;a href="https://www.typescriptlang.org/docs/handbook/utility-types.html" rel="noopener noreferrer"&gt;Utility Types&lt;/a&gt; if you want to select/exclude more than one entry of a type instead of repeating them.&lt;/p&gt;

&lt;p&gt;That's all! I hope you liked my post. Feel free to leave your feedback on this topic.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>The Power of Reduce: Optimizing JavaScript Code for Speed and Efficiency</title>
      <dc:creator>Diogo Almeida</dc:creator>
      <pubDate>Tue, 14 May 2024 08:37:30 +0000</pubDate>
      <link>https://dev.to/red-dial/the-power-of-reduce-optimizing-javascript-code-for-speed-and-efficiency-3jba</link>
      <guid>https://dev.to/red-dial/the-power-of-reduce-optimizing-javascript-code-for-speed-and-efficiency-3jba</guid>
      <description>&lt;p&gt;Hey everybody! This is my first-ever post!&lt;/p&gt;

&lt;p&gt;In this article, I will be talking about the &lt;strong&gt;reduce&lt;/strong&gt; method of &lt;em&gt;JavaScript arrays&lt;/em&gt;, which I feel is sometimes forgotten. I will give a brief introduction to this method, and then I will proceed to compare it with other iteration methods. &lt;/p&gt;

&lt;h2&gt;
  
  
  TLDR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Review your array iterations&lt;/li&gt;
&lt;li&gt;Instead of &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;filter&lt;/code&gt;, use &lt;code&gt;reduce&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Spreading&lt;/em&gt; (as in &lt;code&gt;[...list]&lt;/code&gt;) large lists is a bad idea; &lt;em&gt;Push&lt;/em&gt; to &lt;code&gt;list instead&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;reduce&lt;/code&gt; is faster than &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;filter&lt;/code&gt;, and &lt;code&gt;for in&lt;/code&gt; for large lists&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Personal Notes
&lt;/h2&gt;

&lt;p&gt;I'm a &lt;em&gt;Frontend Developer&lt;/em&gt; with less than 2 years of experience and this is my &lt;u&gt;first article&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;I'm saying this to let you know that I'm still very new, so all feedback is welcome, whether it's about technical content, writing, or personality (please keep it constructive).&lt;/p&gt;

&lt;h2&gt;
  
  
  Motivation: the path to a single loop
&lt;/h2&gt;

&lt;p&gt;While reviewing some people's code (and sometimes my own 😬), I realized that it's really easy to employ &lt;u&gt;unnecessary iterations of arrays&lt;/u&gt; and that sometimes &lt;code&gt;reduce&lt;/code&gt; could improve the code.&lt;/p&gt;

&lt;p&gt;For example, if I have a list of people and I want to get the list of names of all adults, it is easy to think: "First I need to &lt;code&gt;filter&lt;/code&gt; the list and get all the adults (age &amp;gt; 18 years in my country) and then I need to &lt;code&gt;map&lt;/code&gt; it and return their names".&lt;br&gt;
I believe that &lt;em&gt;arrow functions&lt;/em&gt; makes this type of thought even easier because it eliminates the need to write &lt;code&gt;function&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;However, what keeps you from iterating the list of people and adding a person's name to an empty list if they are an adult. &lt;u&gt;This is &lt;code&gt;reduce&lt;/code&gt; in a nutshell&lt;/u&gt;.&lt;br&gt;
This can also be done with ease using a &lt;code&gt;for&lt;/code&gt; loop, although occasionally people overlook this method as well.&lt;/p&gt;


&lt;h2&gt;
  
  
  &lt;em&gt;Reduce&lt;/em&gt; method introduction
&lt;/h2&gt;

&lt;p&gt;I will now give a short introduction to how the &lt;code&gt;reduce&lt;/code&gt; method works, so if you are already familiar with it, be sure to jump to the benchmark section.&lt;/p&gt;
&lt;h3&gt;
  
  
  Theory
&lt;/h3&gt;

&lt;p&gt;In JavaScript, we have &lt;u&gt;more than one way to iterate an &lt;em&gt;array&lt;/em&gt;&lt;/u&gt;, like &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt;, or &lt;code&gt;reduce&lt;/code&gt;. The last one is perhaps the less 'friendly' and common between the three, but instrumental as well, as we shall see. &lt;/p&gt;

&lt;p&gt;The method receives two arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;em&gt;callback function&lt;/em&gt;, that will be used to return the accumulated value in each iteration, and&lt;/li&gt;
&lt;li&gt;an initial value, which will be used to start the accumulation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The callback function itself receives up to four arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;accumulator&lt;/code&gt;, sometimes referred to as &lt;code&gt;total&lt;/code&gt; or abbreviated to &lt;code&gt;acc&lt;/code&gt;, is the value that is "added" onto each iteration;&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;currentValue&lt;/code&gt;, i.e, the current element of the &lt;em&gt;array&lt;/em&gt; being iterated&lt;/li&gt;
&lt;li&gt;Optionally, the &lt;code&gt;currentIndex&lt;/code&gt;, the position of the &lt;code&gt;currentValue&lt;/code&gt; in the &lt;em&gt;array&lt;/em&gt;. Commonly used;&lt;/li&gt;
&lt;li&gt;Optionally, the &lt;em&gt;array&lt;/em&gt; being iterated. It's rarely used since you typically are iterating a variable that you declared previously.
It is useful nonetheless because if you chain methods (e.g. you call &lt;code&gt;initialArray.sort(...).reduce(...)&lt;/code&gt;), then the &lt;em&gt;array&lt;/em&gt; parameter will be the sorted array and not the initial one.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You might notice that I'm employing terms related to &lt;em&gt;accumulate&lt;/em&gt;.  This is because that is what reduce does: it starts with one value, and each iteration it will "add" onto that value (I added quotation marks because algebraic addition is not necessarily involved). &lt;/p&gt;

&lt;p&gt;Let's look at some code:&lt;/p&gt;
&lt;h3&gt;
  
  
  Practical Examples
&lt;/h3&gt;

&lt;p&gt;I'm just going to give you some basic examples of how to use reduce because my goal isn't to turn you into &lt;code&gt;reduce&lt;/code&gt; expert (nor do I claim to be) but to keep this method in the back of your mind.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Retrieving the names of all Adults&lt;/strong&gt;&lt;br&gt;
Let's focus on the previous example. If you were to do the same with &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;filter&lt;/code&gt;, you would do something 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;peopleList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;person&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;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;adult&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;adult&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;reduce&lt;/code&gt;, you'll do 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="nx"&gt;peopleList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;acc&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;person&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;acc&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;This way, you only use one loop instead of two. Is it less 'friendly'? You tell me.&lt;/p&gt;

&lt;p&gt;There are a lot more uses of &lt;code&gt;reduce&lt;/code&gt;, and ways of using it. I opted to use an &lt;code&gt;arrow function&lt;/code&gt; but you don't have to if you don't want. In this &lt;a href="https://codepen.io/Diogo-Almeida-the-vuer/pen/dyLdJZY" rel="noopener noreferrer"&gt;CodePen&lt;/a&gt; you can find:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A comparison of the retrieval of the adults' names between &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;for in&lt;/code&gt; and &lt;code&gt;reduce&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Another example, is where you sum all the numbers in a list. This is a typical example introduced when using &lt;code&gt;reduce&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The reason I'm not detailing it here is that I don't want the concept "&lt;code&gt;reduce&lt;/code&gt; is for sums only" to settle in your head. Yes, it is used for that, but not exclusively, as a teacher once told me.&lt;/p&gt;

&lt;p&gt;In this example, I also detail how you can use &lt;code&gt;reduce&lt;/code&gt; with regular functions and inline returns;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Benchmarking iteration methods
&lt;/h2&gt;

&lt;p&gt;Now, those who know me know that I am a bit obsessed with avoiding repetition and increasing speed and performance. &lt;/p&gt;

&lt;p&gt;This is the origin of my article. &lt;/p&gt;

&lt;p&gt;I wanted to test if there was any difference performance-wise between &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;filter&lt;/code&gt;, and &lt;code&gt;reduce&lt;/code&gt;. Then I decided to throw &lt;code&gt;for in&lt;/code&gt; in the mix as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Always learning!
&lt;/h3&gt;

&lt;p&gt;So I did an initial benchmark and I quickly found a problem:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;⚠ &lt;em&gt;Spreading&lt;/em&gt; slows the &lt;code&gt;reduce&lt;/code&gt; method!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;See, in the &lt;em&gt;reducer function&lt;/em&gt;, I was returning the &lt;code&gt;acc&lt;/code&gt; the following way:&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;return&lt;/span&gt; &lt;span class="nx"&gt;condition&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With short lists, this isn't much of a problem, but with colossal-sized lists, this syntax made the &lt;code&gt;reduce&lt;/code&gt; method much slower (as you will see).&lt;/p&gt;

&lt;p&gt;Imagine my surprise when I first developed the benchmark and saw that &lt;code&gt;reduce&lt;/code&gt; (the method I am advocating in my article) was much slower than &lt;code&gt;map and filter&lt;/code&gt;! However, after some research, I found out that this was because in each iteration an object is created, and thousands of elements are &lt;em&gt;spreaded&lt;/em&gt; onto it, so a strategy change was needed:&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;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;acc&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;item&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;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, whenever I employ &lt;code&gt;reduce&lt;/code&gt; in my projects, instead of &lt;em&gt;spreading&lt;/em&gt;, I &lt;em&gt;push&lt;/em&gt; the item to the &lt;code&gt;acc&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The benchmark
&lt;/h3&gt;

&lt;p&gt;So, to develop my benchmark I created another &lt;a href="https://codepen.io/Diogo-Almeida-the-vuer/pen/LYvOjGN" rel="noopener noreferrer"&gt;CodePen&lt;/a&gt; where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A list of 100,000 random numbers is created;&lt;/li&gt;
&lt;li&gt;Each method runs a certain task 100 times, and each execution is timed;&lt;/li&gt;
&lt;li&gt;And an average is obtained for each method;
Regardless of the iteration and method, the task itself is to obtain the binary converted numbers smaller than 16. It's not a "real world" example, but it'll do for this purpose.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You'll see that I added the &lt;code&gt;reduce&lt;/code&gt; method with &lt;em&gt;spread&lt;/em&gt; and &lt;em&gt;push&lt;/em&gt; so that you can see the difference in performance. So let's see what the fastest method is.&lt;/p&gt;

&lt;h3&gt;
  
  
  Results: And the winner is...
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Reduce&lt;/strong&gt;! Of course, it's &lt;code&gt;reduce&lt;/code&gt;. I wouldn't be making this article if it wasn't. 😄&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcnhafmgnak84mgoil2g5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcnhafmgnak84mgoil2g5.png" alt="Iteration methods time comparison" width="526" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You'll notice I didn't add the &lt;code&gt;reduce&lt;/code&gt; with &lt;em&gt;spread&lt;/em&gt; time to the graphic because it was &lt;u&gt;8.8 &lt;strong&gt;seconds&lt;/strong&gt; long&lt;/u&gt; (over 3000 times slower than with push)! All other methods are measured in &lt;strong&gt;milliseconds&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As you can see, the difference between the three iteration methods is not very large, but it doesn't change the fact that &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;filter&lt;/code&gt; are 3 milliseconds slower than &lt;code&gt;reduce&lt;/code&gt;. Of course, with smaller lists, the difference will be minimal. &lt;/p&gt;

&lt;p&gt;&lt;u&gt;Some things to consider&lt;/u&gt; about this benchmark are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The code may seem overcomplicated, but that was the best way I found to avoid re-creating functions with each iteration or method.&lt;/li&gt;
&lt;li&gt;I pushed the list's length to reaches that probably aren't reasonable. It's rare the project where one deals with lists with thousands of elements. But if you are, and performance is a big issue in your website, &lt;em&gt;reduce&lt;/em&gt; is your friend;&lt;/li&gt;
&lt;li&gt;Dealing with big lists isn't usually handled in pure JS. There are several ways to handle them, such as &lt;em&gt;virtualization&lt;/em&gt; or &lt;em&gt;pagination&lt;/em&gt;, so be warned.&lt;/li&gt;
&lt;li&gt;The times I presented were obtained by running the code on &lt;em&gt;CodePen&lt;/em&gt;, so in production, on a server, the execution can be faster.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;That's all folks! &lt;br&gt;
Thank you for reading my first post, and I hope you found it useful in some way (I don't refund the time you spent on it otherwise)&lt;/p&gt;

&lt;h2&gt;
  
  
  References and links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/jsref/jsref_reduce.asp" rel="noopener noreferrer"&gt;W3 Schools&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://codepen.io/Diogo-Almeida-the-vuer/pen/dyLdJZY?editors=0012" rel="noopener noreferrer"&gt;Reduce Introduction CodePen&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://codepen.io/Diogo-Almeida-the-vuer/pen/LYvOjGN?editors=0010" rel="noopener noreferrer"&gt;Reduce Benchmark CodePen&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

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