<?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: Robin Lejeune</title>
    <description>The latest articles on DEV Community by Robin Lejeune (@robinlej).</description>
    <link>https://dev.to/robinlej</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%2F819104%2F79b45901-6d43-4e08-b11c-b986ee2df26a.jpeg</url>
      <title>DEV Community: Robin Lejeune</title>
      <link>https://dev.to/robinlej</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/robinlej"/>
    <language>en</language>
    <item>
      <title>Iterating on an array: should you loop with .map, .forEach or for?</title>
      <dc:creator>Robin Lejeune</dc:creator>
      <pubDate>Tue, 31 Jan 2023 23:17:18 +0000</pubDate>
      <link>https://dev.to/robinlej/iterating-on-an-array-should-you-loop-with-map-foreach-or-for-5fim</link>
      <guid>https://dev.to/robinlej/iterating-on-an-array-should-you-loop-with-map-foreach-or-for-5fim</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;TL;DR? Check at least the sections "Time for the comparisons!" and "But wait! There's more" ;)&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;p&gt;1. The Problem&lt;br&gt;
2. How Do We Tackle That?&lt;br&gt;
3. My First Thoughts&lt;br&gt;
4. Improve on The First Try&lt;br&gt;
5. Comparison Between .forEach(), .map() and for&lt;br&gt;
6. Time For The Comparisons!&lt;br&gt;
7. But Wait! There's More&lt;br&gt;
8. Conclusion&lt;/p&gt;



&lt;p&gt;I was recently asked to answer a coding exercise before a job interview. Out of the 3 questions, one felt particularly easy as I coded the answer in 5 minutes, but then... Then a problem occurred.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;My code passed 13 out of 15 tests. For the last two, it would not give an output in due time. It was not optimized enough (&lt;em&gt;it wasn't optimized at all, if you ask me&lt;/em&gt;). Unfortunately, I only had 1 min left, and I couldn't find a proper solution in time. But then I thought some more, imagined a few possible optimized solutions, and ran a few tests of my own to verify them.&lt;/p&gt;

&lt;p&gt;Here is the exercise: from a given array filled with numbers, you should return all the possible combinations which sum matches a given target (but return each possible combination only once).&lt;br&gt;
For instance, from &lt;code&gt;arr = [1, 5, 2, 4, 4]&lt;/code&gt; with &lt;code&gt;target = 6&lt;/code&gt;, you should return &lt;code&gt;[1, 5], [2, 4]&lt;/code&gt; (or &lt;code&gt;'4,2', '5,1'&lt;/code&gt;, the way in which it is presented and the order don't matter). Easy enough.&lt;br&gt;
Of course, said array could be much bigger (say 20000 elements). This is when optimization kicks in.&lt;/p&gt;


&lt;h2&gt;
  
  
  How Do We Tackle That?
&lt;/h2&gt;

&lt;p&gt;What do we have to solve that kind of problem? &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Array.map()&lt;/code&gt; and &lt;code&gt;Array.flatMap()&lt;/code&gt;, which take a callback and create a new array from the existing one. (&lt;code&gt;.flatMap()&lt;/code&gt; returns, as its name suggests, a flattened array.)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Array.forEach()&lt;/code&gt;, which also takes a callback, loops through the array, and runs whatever you ask it to run (say, &lt;code&gt;.push()&lt;/code&gt; the result in another empty array).&lt;/li&gt;
&lt;li&gt;Or a plain, basic &lt;code&gt;for&lt;/code&gt; loop. In the end, for what we are after, it works a bit like a &lt;code&gt;.forEach()&lt;/code&gt;: you first create an empty array, then push whatever the loop returns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From here, we don't have so many options. We have one array, where each element must be associated with every other element exactly once if the sum is correct. What this means is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We need a new array&lt;/li&gt;
&lt;li&gt;We need to loop over the array, to get each element&lt;/li&gt;
&lt;li&gt;And we need to loop a second time, to get the second element&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A loop inside a loop, that's never ideal. But at this point I couldn't find a better solution.&lt;/p&gt;



&lt;p&gt;
  There is actually a way to stay on a linear time method
  &lt;br&gt;
Thanks to Vincent A. Cicirello in the comments who pointed this out to me.

&lt;p&gt;If you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;generate a Set A out of the initial array,&lt;/li&gt;
&lt;li&gt;then iterate over each value and store &lt;code&gt;y = target - value&lt;/code&gt; in another array or Set B,&lt;/li&gt;
&lt;li&gt;then iterate over B and compare y to the values in A, and store the existing pairs &lt;code&gt;[y, target - value]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;and finally check whether the initial array contains &lt;code&gt;target / 2&lt;/code&gt; at least twice (because if your target is 6, &lt;code&gt;[3, 3]&lt;/code&gt; is a valid answer; but if you only have one 3 in the array, you should remove &lt;code&gt;[3, 3]&lt;/code&gt; from the pairs)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your function takes way less time. That is better and smarter in every aspect than what I'm developing here under.&lt;br&gt;
I won't linger on this in the article, though, because I want to compare &lt;code&gt;.forEach()&lt;/code&gt;, &lt;code&gt;.map()&lt;/code&gt; and &lt;code&gt;for&lt;/code&gt; in a similar environment, which is here a quadratic time one.&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;
&lt;/p&gt;




&lt;h2&gt;
  
  
  My First Thoughts
&lt;/h2&gt;

&lt;p&gt;Ok, we know where we're heading.&lt;/p&gt;

&lt;p&gt;As we are dealing with an array, my first thoughts were to use a &lt;code&gt;.forEach&lt;/code&gt;. (&lt;em&gt;Don't ask me why I didn't start with a &lt;code&gt;.flatMap&lt;/code&gt;, which seemed called for since we are basically&lt;/em&gt; returning &lt;em&gt;a new array. My excuse is that I was stressed.&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;Here's my code, commented for the beginners among you:&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;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// I initiate an empty array&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pairs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="c1"&gt;// I loop once over arr&lt;/span&gt;
    &lt;span class="c1"&gt;// I keep in memory the element (e) and its index (i)&lt;/span&gt;
    &lt;span class="nx"&gt;arr&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;e&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// I loop a second time over arr, &lt;/span&gt;
        &lt;span class="c1"&gt;// and similarly keep in memory the element `el` and its index `j`&lt;/span&gt;
        &lt;span class="nx"&gt;arr&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;el&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;j&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="c1"&gt;// I compare `i` and `j` to make sure I'm not &lt;/span&gt;
            &lt;span class="c1"&gt;// combining the same element with itself,&lt;/span&gt;
            &lt;span class="c1"&gt;// and I check that the sum meets the target&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;i&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;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// By using a ternary operator to assign a value,&lt;/span&gt;
                &lt;span class="c1"&gt;// I make sure each pair is presented &lt;/span&gt;
                &lt;span class="c1"&gt;// with the smallest number first&lt;/span&gt;
                &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pair&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                    &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt;
                        &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&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;el&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="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="c1"&gt;// Two similar arrays [1, 2] are two distinct objects.&lt;/span&gt;
                &lt;span class="c1"&gt;// To compare them, I use a trick with `JSON.stringify()`,&lt;/span&gt;
                &lt;span class="c1"&gt;// because... Well, because strings are comparable.&lt;/span&gt;
                &lt;span class="nx"&gt;pairs&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;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;pair&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="c1"&gt;// Finally, I return a set from the array,&lt;/span&gt;
    &lt;span class="c1"&gt;// to make sure I have each combination only once&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pairs&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;
  
  
  Improve on The First Try
&lt;/h2&gt;

&lt;p&gt;You might see a few problems with my function already.&lt;/p&gt;

&lt;p&gt;For instance, why stringify the combined array? We could just return a string. Remember: the form doesn't matter. That's a good guess: removing &lt;code&gt;JSON.stringify()&lt;/code&gt; means removing a function, so less computing time. This is not the crucial point, though.&lt;/p&gt;

&lt;p&gt;The true problem here is that I loop over elements which have already been used up. Take &lt;code&gt;[1, 2, 3]&lt;/code&gt;: once I returned &lt;code&gt;[1, 2]&lt;/code&gt;, I don't need to return &lt;code&gt;[2, 1]&lt;/code&gt;. Instead of looping over the whole array the second time, I could start at &lt;code&gt;i + 1&lt;/code&gt;, the index of the current element + 1. Let's use &lt;code&gt;.slice(i + 1)&lt;/code&gt; to do it.&lt;/p&gt;

&lt;p&gt;One could even argue that the &lt;code&gt;Set&lt;/code&gt; is now useless. That's true if all the elements in the array are different, but not if you have duplicates. So let's keep it.&lt;/p&gt;

&lt;p&gt;Here's the better version:&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;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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;pairs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&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;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;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="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;j&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;e&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&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;pair&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                    &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;el&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;e&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;el&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;el&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;e&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
                &lt;span class="nx"&gt;pairs&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;pair&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pairs&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;
  
  
  Comparison Between .forEach(), .map() and for
&lt;/h2&gt;

&lt;p&gt;That's better, but it wasn't good enough to pass the last 2 tests. I didn't have time to do better, but after the timer stopped, I ran some checks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So what do you think? Which one of &lt;code&gt;.forEach()&lt;/code&gt;, &lt;code&gt;.map()&lt;/code&gt; or &lt;code&gt;for&lt;/code&gt; is the fastest, and to which extent?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's try. We will run &lt;code&gt;test1()&lt;/code&gt;, &lt;code&gt;test2()&lt;/code&gt; and &lt;code&gt;test3()&lt;/code&gt; to see the difference.&lt;/p&gt;

&lt;p&gt;First, we initiate our array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// An array of 10000 elements, each corresponding to its index&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&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;i&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;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Let's double the array for fun (and to create duplicates)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pirate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first test will check the time taken for &lt;code&gt;.forEach()&lt;/code&gt;.&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;test1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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;startTime&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;Date&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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;pairs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

        &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&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;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;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="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;j&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;e&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&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;pair&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                        &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;el&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;e&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;el&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;el&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;e&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
                    &lt;span class="nx"&gt;pairs&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;pair&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pairs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;startTime&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'll check the time taken for a &lt;code&gt;.map()&lt;/code&gt;&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;test2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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;startTime&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;Date&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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;pairs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flatMap&lt;/span&gt;&lt;span class="p"&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;i&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;arr&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;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="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;el&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;e&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&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;e&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;el&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;e&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;el&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;el&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;e&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="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;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;e&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;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pairs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;startTime&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we'll test the simple &lt;code&gt;for&lt;/code&gt; loop.&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;test3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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;startTime&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;Date&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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;pairs&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;arr&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="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="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;j&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;1&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;arr&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="nx"&gt;j&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&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;pair&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&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="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&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;arr&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="s2"&gt;, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&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;arr&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="s2"&gt;`&lt;/span&gt;

                    &lt;span class="nx"&gt;pairs&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;pair&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pairs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;startTime&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Time For The Comparisons!
&lt;/h2&gt;

&lt;p&gt;It's time for the results. But first: what are you expecting? What would your podium be?&lt;/p&gt;

&lt;p&gt;To avoid exceptions, I run each test 10 times and calculate the average delay (in ms). Which gives us:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;forEach()&lt;/th&gt;
&lt;th&gt;map()&lt;/th&gt;
&lt;th&gt;for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;3212.9&lt;/td&gt;
&lt;td&gt;16618.1&lt;/td&gt;
&lt;td&gt;787.2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So it seems we have a winner. A really, really clear winner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Are you as astonished as I was?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I mean, OK, for a seasoned developer, the victory of the &lt;code&gt;for&lt;/code&gt; loop won't be much of a surprise. It is well documented after all. But to this extent? Boy, was I not expecting that!&lt;/p&gt;

&lt;p&gt;It turns out the &lt;code&gt;for&lt;/code&gt; loop is more than 4 times faster than &lt;code&gt;.forEach()&lt;/code&gt; and 21 times faster than the &lt;code&gt;.map()&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;In fact, the margin is so big that even if I test the solutions without slicing the array to the current element on the second loop, they remain better than other options. In the end, the fastest solutions are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;for&lt;/code&gt; (with &lt;code&gt;j = i + 1&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;for&lt;/code&gt; (with &lt;code&gt;j = 0&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.forEach()&lt;/code&gt; with &lt;code&gt;.slice()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.forEach()&lt;/code&gt; without &lt;code&gt;.slice()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.map()&lt;/code&gt; with &lt;code&gt;.slice()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.map()&lt;/code&gt; without &lt;code&gt;.slice()&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
  Update on the linear time solution
  &lt;br&gt;
It doesn't make much sense to compare this to the previous results, but just for fun, how long does a truly optimized function take?&lt;br&gt;
Let's code the method suggested by Vincent A. Cicirello in the comments:&lt;br&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;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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="kd"&gt;set&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;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pairs&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;Set&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;potentialPairings&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;Set&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="kd"&gt;set&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;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;potentialPairings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&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;potentialPairings&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;pairing&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="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pairing&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;pair&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="nx"&gt;pairing&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;target&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;pairing&lt;/span&gt;&lt;span class="p"&gt;)&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;target&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;pairing&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;pairing&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;pairing&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;target&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;pairing&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
            &lt;span class="nx"&gt;pairs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pair&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&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;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;pairs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&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;target&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="s2"&gt;, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;target&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="s2"&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;pairs&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;With 10000 items duplicated, this takes a whopping 5 ms to compute.&lt;br&gt;
And what do we learn from this, kids? That you should really, reaaaally put some efforts in keeping a linear time function. Don't follow my lead.&lt;/p&gt;



&lt;/p&gt;




&lt;h2&gt;
  
  
  But Wait! There's More
&lt;/h2&gt;

&lt;p&gt;What could there be left to be said? We have a podium, and that article is already too long!&lt;/p&gt;

&lt;p&gt;Well, as I was writing this, I found out that the results are very, very different if you remove the target. Basically, instead of returning every pair that matches a target, you return every single possible pair. That obviously takes a lot more time to compute.&lt;/p&gt;

&lt;p&gt;Because I like my laptop, I tested with an array of 3000 instead of 10000. Here are the results:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;forEach()&lt;/th&gt;
&lt;th&gt;map()&lt;/th&gt;
&lt;th&gt;for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;9147.1&lt;/td&gt;
&lt;td&gt;9097.4&lt;/td&gt;
&lt;td&gt;8892.2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The results are much tighter. &lt;code&gt;for&lt;/code&gt; remains the winner, but in this case all three options are fairly similar - and &lt;code&gt;.map()&lt;/code&gt; is consistently, although slightly, better than &lt;code&gt;.forEach()&lt;/code&gt;.&lt;/p&gt;




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

&lt;p&gt;Two things are certain:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If you can find a way, remove those nested loops.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;for&lt;/code&gt; is your go-to if you need an optimization boost and you cannot reduce the number of nested loops. Additionnally, the less matches it finds, the faster &lt;code&gt;for&lt;/code&gt; gets, and the bigger the gap between &lt;code&gt;for&lt;/code&gt; and the rest. So if you have a very specific condition at the start of the second loop, it will trim the computed time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For the rest, to be honest, I'm still trying to figure out where the huge differences in results comes from - and I'm talking of the difference between the 3 options in the first tests, as well as the difference between the first tests and the second ones. If you have an answer or an assumption, please leave a comment, I'll take it! :)&lt;/p&gt;

</description>
      <category>workplace</category>
      <category>design</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Web Accessibility – What it is, why you should pay attention to it and how to get started</title>
      <dc:creator>Robin Lejeune</dc:creator>
      <pubDate>Fri, 10 Jun 2022 07:35:34 +0000</pubDate>
      <link>https://dev.to/robinlej/web-accessibility-what-it-is-why-you-should-pay-attention-to-it-and-how-to-get-started-1mmo</link>
      <guid>https://dev.to/robinlej/web-accessibility-what-it-is-why-you-should-pay-attention-to-it-and-how-to-get-started-1mmo</guid>
      <description>&lt;p&gt;What’s the common point between semantic HTML, the animations of your components and a dark mode?&lt;/p&gt;

&lt;p&gt;Well, I guess the title of the article spoiled the surprise for you: all those elements (and a lot more) can make or hinder your website &lt;em&gt;accessibility&lt;/em&gt;. Which is cool to know and all, but what does "accessible" really mean anyways?&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s take a little detour into the physical world
&lt;/h2&gt;

&lt;p&gt;Just think of what an accessible city looks like. What comes to mind?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Disabled parking places, wider than standard ones and situated in close vicinity of the building they serve.&lt;/li&gt;
&lt;li&gt;Sloped sidewalks next to the crosswalks.&lt;/li&gt;
&lt;li&gt;Elevators and access ramps instead of stairs, and railings on the stairs.&lt;/li&gt;
&lt;li&gt;Different sounds emitted by the trafic lights whether it’s green or red for the pedestrians.&lt;/li&gt;
&lt;li&gt;Ribbed pavement, that helps visually impaired people find their way around.&lt;/li&gt;
&lt;li&gt;You could also consider wider pavements and avoiding paved roads which are unstable.&lt;/li&gt;
&lt;li&gt;Etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You see the picture. The idea here is not to make an exhaustive list, but to point out how accessibility sets a global context to keep in mind that impacts lots of aspects, and can take lots of forms.&lt;/p&gt;

&lt;p&gt;What you might not be aware of, is that most disabilities are invisible. Of course you see that woman in a wheelchair or the blind guy with a cane, but how do you spot a deaf person? How can you know that the man with the glasses, 10 meters away from you, is actually heavily near-sighted? How can you know if a person has a chronic disease that prevents them from doing everything an abled person can do? You can’t (except maybe if you creepily watch them for several minutes – and please don’t). And the conclusion of that observation is that &lt;strong&gt;all those infrastructures are actually useful for a lot more people than you would think of&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now, another thing to have in mind is that, although they primarily focus on alleviating the lives of disabled people, &lt;strong&gt;all those features make the city also more accessible to everybody&lt;/strong&gt;. And the reason behind that is, very simply, that anybody can be at least temporarily impaired, or impeded, at some point in their life. Elderly, pregnant women, parents with a stroller, teenagers with a broken leg: they benefit from most adapted infrastructures. And I say they, but I really should be saying you. Even if you’re not in any of these situations, I’m sure you feel tired from times to times, and you happen to take the elevator at the train station when you could have taken the stairs. Or that you see very easily the positive impact wider pavements can have.&lt;/p&gt;

&lt;h2&gt;
  
  
  Back to the web
&lt;/h2&gt;

&lt;p&gt;Ok, but let’s go back to our subject: how is any of this relevant to web accessibility? Well, the whole picture translates very directly to the web: 1) there is a lot you can do to make the web a warmer, more welcoming place, 2) the possibilities take very different forms, 3) it is more important than you think, because you underestimate the number of people who need it, and 4) everybody, you included even if you're not disabled, actually benefits from them in the end.&lt;/p&gt;




&lt;h2&gt;
  
  
  How do others experience the web?
&lt;/h2&gt;

&lt;p&gt;Before asking yourself what you should do, begin by understanding why you should do something. In other words, how do others experience the web? We can roughly distribute disabilities in 4 categories:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Visual disabilities&lt;/li&gt;
&lt;li&gt;Auditory disabilities&lt;/li&gt;
&lt;li&gt;Motor disabilities&lt;/li&gt;
&lt;li&gt;Cognitive disabilities&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Visual disabilities&lt;/strong&gt; can mean:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Blindness, which translates for a web developer into making sure a screen reader will be able to deliver meaningful content from your pages (alt text on images, every interaction should be reachable with the keyboard, using semantic HTML tags and ARIA when needed, the order of your HTML elements should be the semantic order of your page, ...).&lt;/li&gt;
&lt;li&gt;Low vision, which calls for bigger fonts and high contrast.&lt;/li&gt;
&lt;li&gt;Photosensitive condition, which can be tackled with a dark mode.&lt;/li&gt;
&lt;li&gt;Colourblindness, which implies that meaning should always be conveyed with something else than just color. A good contrast helps here too.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Want to know how it feels to use a screen reader? &lt;a href="https://www.youtube.com/watch?v=IhWMou12_Vk"&gt;Check out this video&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Auditory disabilities&lt;/strong&gt; call for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Subtitles on your videos&lt;/li&gt;
&lt;li&gt;Transcripts of your podcasts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Motor disabilities&lt;/strong&gt; can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fine motor impairments, that for instance make it difficult to click on a button or a link if it’s too small.&lt;/li&gt;
&lt;li&gt;Lack of muscle control, making it impossible to use a mouse, but still possible to use keyboard navigation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cognitive disabilities&lt;/strong&gt; are a wide spectrum:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vertigo or headaches provoked by motion sickness&lt;/li&gt;
&lt;li&gt;Difficulty to focus if a page is too loaded with elements&lt;/li&gt;
&lt;li&gt;Uncontrolled anxiety induced by unexpected behaviours, such as auto-plays, pop-ups, fancy scrolling behaviours, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;See &lt;a href="https://source.opennews.org/articles/motion-sick/"&gt;this article, on the author's daily experience on the web&lt;/a&gt; or &lt;a href="https://alistapart.com/article/accessibility-for-vestibular/"&gt;this other one, where the author experienced temporary impairment&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Of course this is only a sample of what is at stake, but I believe it already paints a pretty generic view. It is also a starting point to see how, by implementing those elements, you can improve the experience for everyone: subtitles on videos are useful for those who can’t or don’t want to turn on the sound (which is very common on smartphones), keyboard navigation for anyone who doesn’t want to go back to their mouse for every action, dark modes are more and more asked for just by sheer preference, auto-play and pop-ups are a plague anyways, etc.&lt;/p&gt;

&lt;p&gt;When you’re reading lists like that, implementing everything, and even more so implementing everything &lt;em&gt;properly&lt;/em&gt;, can feel like a daunting task. So let me tell you this: no-one expects you to do everything at once. Take it step by step, one feature at a time. Learn how one thing works, put it into practice, and little by little you’ll learn more and won't have to think about it too much. I myself am by no means an expert in the field – but I try to be aware of these considerations when coding.&lt;/p&gt;

&lt;p&gt;Please bear that in mind as we enter the next section: you don’t need to go from not knowing anything to mastering everything. You’ll make some mistakes, you won’t be able to implement everything, and it’s fine, as long as you’re willing to learn and improve your knowledge. Doing a little to make the web a better place will always be better than not doing anything at all.&lt;/p&gt;




&lt;h2&gt;
  
  
  How do I implement that?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  SEMANTIC HTML
&lt;/h3&gt;

&lt;h4&gt;
  
  
  BLOCK-LEVEL ELEMENTS
&lt;/h4&gt;

&lt;p&gt;You know about semantic HTML, but do you use it? You might have learnt about &lt;code&gt;&amp;lt;main&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;aside&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;article&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;nav&amp;gt;&lt;/code&gt; and the others at some point, maybe very quickly in the first week of a bootcamp, but then you focused on other things and never thought about them again. Now &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; is your best friend, and everything is a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;. Let’s question why they are actually useful and why you shouldn’t set divs everywhere.&lt;/p&gt;

&lt;p&gt;For a screen reader, those tags contain, well, &lt;em&gt;semantic meanings&lt;/em&gt;. Imagine a page full of divs: how could an assistive technology know which div does what? I’m sure you gave them &lt;em&gt;very explicit&lt;/em&gt; class names, like &lt;code&gt;.container&lt;/code&gt; and &lt;code&gt;.content&lt;/code&gt;. You’ll agree with me that it’s basically useless in order to distinguish their uses. Note that even if you have more meaningful classes, like &lt;code&gt;.header&lt;/code&gt; and &lt;code&gt;.main&lt;/code&gt;, there is no standard for it, so you can’t expect the screen reader to understand what you mean with that. Plus, if those are the names of your classes, you might as well use the actual HTML elements.&lt;/p&gt;

&lt;p&gt;On the contrary, since it’s standard, what your &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; is is very clear, and will be clearly presented as such to the user. Similarly, a &lt;code&gt;&amp;lt;nav&amp;gt;&lt;/code&gt; will be notified to the user as a navigation element. That makes it much clearer than just knowing it’s a list.&lt;/p&gt;

&lt;p&gt;Additionnally, search engines love semantic HTML too, and your site will end up much higher on Google if you use appropriate semantics. See? I told you accessibility benefits everybody.&lt;/p&gt;

&lt;h4&gt;
  
  
  COMPONENTS ELEMENTS
&lt;/h4&gt;

&lt;p&gt;But there is more to semantic HTML than those block-level elements. A &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; is not a link &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt;, and vice versa. If you don’t know the difference: a link should take you to another page, while a button suggests an interaction (submitting a form, opening a pop-up, counting the sheep…). If your button says "Discover who we are", chances are it’s actually a link to your "About" page: use a &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; and style it like a button if you want to. Conversely, if you have a "Read more..." styled like a link that expands your article like an accordion, consider it is a button. A good rule of thumb you’ve already heard but may forget from time to time: always use CSS to style, not HTML elements.&lt;/p&gt;

&lt;h4&gt;
  
  
  TITLES
&lt;/h4&gt;

&lt;p&gt;Other semantic HTML elements include the title tags, &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; to &lt;code&gt;&amp;lt;h6&amp;gt;&lt;/code&gt;. In summary, here are the basics you should know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You should only have one &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; per page. This is your main headline, the page title, what makes it different from another page on your website. Everything else on the page is a subtitle.&lt;/li&gt;
&lt;li&gt;(Sub)titles should be used incrementally. After an &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; comes &lt;code&gt;&amp;lt;h2&amp;gt;&lt;/code&gt;, and after that comes &lt;code&gt;&amp;lt;h3&amp;gt;&lt;/code&gt;, not &lt;code&gt;&amp;lt;h5&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Here again, don't use them based on what they look like; use them based on their semantics and style them afterwards.&lt;/li&gt;
&lt;li&gt;It is also a good SEO practice (Search Engine Optimization, the technique to make Google happy and appear in the 5 first results).&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  WCAG, ARIA, A11Y: WTF?
&lt;/h3&gt;

&lt;p&gt;You probably heard before of WCAG compliance, ARIA or a11y, but their meaning and purpose might be blurry to you (and who could blame you with all those obscure acronyms?).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.w3.org/TR/WCAG21/"&gt;&lt;strong&gt;WCAG&lt;/strong&gt;&lt;/a&gt; stands for &lt;em&gt;Web Content Accessibility Guidelines&lt;/em&gt;. Those have been formulated by the World Wide Web Consortium (W3C) in 1999 and have been evolving since then, just like the web in itself. The last version of the recommendations (2.1) was published in 2018 and the very complete documentation is to be found on &lt;a href="https://www.w3.org/TR/WCAG21/"&gt;https://www.w3.org/TR/WCAG21/&lt;/a&gt;. It gives indications on the expected behaviour of pretty much anything that can be found on the web, in order to be as accessible as possible (levels A, AA, AAA). These have an impact on things from alt text to focus states, modals to videos, keyboard interaction to color contrast. On some of these subjects, here are some good reads:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.sarasoueidan.com/blog/focus-indicators/"&gt;How to design good focus indicators?&lt;/a&gt; Sara Soueidan gives extremely valuable advice. &lt;/li&gt;
&lt;li&gt;Should you use &lt;code&gt;px&lt;/code&gt;, &lt;code&gt;em&lt;/code&gt; or &lt;code&gt;rem&lt;/code&gt; units? &lt;a href="https://www.joshwcomeau.com/css/surprising-truth-about-pixels-and-accessibility/"&gt;This article by Josh Comeau&lt;/a&gt; will help you wrap your head around the differences between them.&lt;/li&gt;
&lt;li&gt;Think about implementing &lt;code&gt;@media (prefers-reduced-motion)&lt;/code&gt; and read &lt;a href="https://web.dev/prefers-reduced-motion/"&gt;why it is important and how to do it&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Think about implementing &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme"&gt;&lt;code&gt;@media (prefers-color-scheme: dark)&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://www.w3.org/TR/wai-aria/"&gt;&lt;strong&gt;ARIA&lt;/strong&gt;&lt;/a&gt; – not the Game of Thrones character – stands for &lt;em&gt;Accessible Rich Internet Applications&lt;/em&gt;. It is one of the ways you can make sure your content is accessible to as many people as possible. It allows you to let screen readers know what they should say when they encounter a specific element on your page. Remember those semantic HTML tags? They use ARIA under the hood without you having to think about it, and that's the reason why they should be used. It is a powerful tool to make sure your website is understandable by everybody, even when used under different conditions. I won’t go into too much details about it, but please remember though that &lt;em&gt;no ARIA is better than bad ARIA&lt;/em&gt;! While it is powerful and a good practice to implement it where it needs to be, it can also do a lot of harm to your accessibility if you don’t use it properly. Always prefer an accurate semantic HTML tag if possible. If not, then ARIA implementation of &lt;a href="https://www.a11yproject.com/posts/an-indepth-guide-to-aria-roles/"&gt;roles&lt;/a&gt; and other attributes is a great way to make your website more accessible:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an input wrapped within a &lt;code&gt;&amp;lt;div role="search"&amp;gt;&lt;/code&gt; makes it clear that it's a searchbar (otherwise it's just a text input for a screen reader)&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;role="dialog"&lt;/code&gt; means there's a component set above everything else on your page (called a &lt;em&gt;dialog&lt;/em&gt; or &lt;em&gt;modal&lt;/em&gt;), and the user should not expect to be able to interact with anything else outside that modal. That last part also means that setting the role is not enough, you need to implement everything around it: 1) &lt;a href="https://medium.com/@islam.sayed8/trap-focus-inside-a-modal-aa5230326c1b"&gt;trap the focus&lt;/a&gt; inside the modal (i.e. disabling the page behind it); 2) the attribute &lt;code&gt;aria-modal="true"&lt;/code&gt; tells the screen reader that, when opened, everything behind the modal is inert; 3) have the &lt;code&gt;ESC&lt;/code&gt; key close the modal and return the focus to where the user was before opening it; 4) possibly make it so that a click on the area around the modal closes it too...&lt;/li&gt;
&lt;li&gt;attributes such as &lt;code&gt;aria-label&lt;/code&gt;, &lt;code&gt;aria-labelledby&lt;/code&gt;, &lt;code&gt;aria-describedby&lt;/code&gt;, etc. can also occasionnally be used to be more explicit and help make sense of what the element is about. See for instance how it can be used with &lt;code&gt;&amp;lt;section&amp;gt;&lt;/code&gt; tags to improve your accessibility &lt;a href="https://youtu.be/ULdkpU51hTQ"&gt;on this video by Kevin Powell&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;For more examples, I highly recommend you to &lt;a href="https://www.w3.org/WAI/ARIA/apg/patterns/"&gt;check this page that identifies patterns&lt;/a&gt; and gives very thorough examples of how to implement ARIA in your projects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://www.a11yproject.com/"&gt;&lt;strong&gt;The A11Y project&lt;/strong&gt;&lt;/a&gt; aims at making accessibility easier. They offer help, posts, resources to help you understand how to make stuff accessible and why it matters. Why A11Y? Well, this one is actually self-explanatory and easy to remember, so I guess you didn't actually ask the question, but it’s also a pretty cunning way of writing a*ccessibilit*y: just count the letters.&lt;/p&gt;




&lt;h2&gt;
  
  
  To summarize
&lt;/h2&gt;

&lt;p&gt;I hope you learnt some things along the way! The idea of this article was mainly to set a general overview of what web accessibility means, why it matters, what it looks like, and give some leads on the tools at the disposal of developers to tackle the issue. Of course, this is but scratching the surface. I didn't discuss lots of existing features and important things to set up. But now I'll leave it to you to dig the subject.&lt;/p&gt;

&lt;p&gt;In addition to the resources scattered across the article, I also advise you to make your own list of experts and not-so-experts developers who talk about accessibility and follow them, either on social media or by checking the RSS feed from their blog if they have one (and yes, RSS is still pretty darn useful in 2022 :) ). It's always easier to learn things over the span of several months, just by reading / listening to great pieces. And this whole accessibility thing might start to look simple over time...&lt;/p&gt;

</description>
      <category>a11y</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
