<?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: Mihail Gaberov</title>
    <description>The latest articles on DEV Community by Mihail Gaberov (@mihailgaberov).</description>
    <link>https://dev.to/mihailgaberov</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%2F50986%2Fc4275676-0e79-43c9-aae0-5e6bd8d09668.jpeg</url>
      <title>DEV Community: Mihail Gaberov</title>
      <link>https://dev.to/mihailgaberov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mihailgaberov"/>
    <language>en</language>
    <item>
      <title>How to visualize Pancakes Algorithm with React and Popmotion.io</title>
      <dc:creator>Mihail Gaberov</dc:creator>
      <pubDate>Tue, 03 Sep 2019 13:03:09 +0000</pubDate>
      <link>https://dev.to/mihailgaberov/how-to-visualize-pancakes-algorithm-with-react-and-popmotion-io-3ddj</link>
      <guid>https://dev.to/mihailgaberov/how-to-visualize-pancakes-algorithm-with-react-and-popmotion-io-3ddj</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jLVSTmCZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mihail-gaberov.eu/static/b2a4c82960de627dfb6204e97dc93813/6fc51/pancakes-algorithm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jLVSTmCZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mihail-gaberov.eu/static/b2a4c82960de627dfb6204e97dc93813/6fc51/pancakes-algorithm.png" alt="Pancakes Algorithm Visualization"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What you are going to see below was suppose to be part of my solution to an exercise given in a coding challenge. It was several months ago and I had signed in for it. Due to unforeseen factors, I haven't gone to the end. Now, after that time and the challenge finished, I can share it here.&lt;/p&gt;

&lt;p&gt;This is not going to be a step-by-step tutorial. Rather a quick review of how can we use frameworks like React and Popmotion.io, and an algorithm. And create a nice visualization of that very same algorithm. Somehow it feels nice! 🤓&lt;/p&gt;

&lt;p&gt;The so called &lt;a href="https://en.wikipedia.org/wiki/Pancake_sorting"&gt;Pancakes Sorting Algorithm&lt;/a&gt; is famous (or not?) sorting algorithm, that you can read about a lot in internet, if interested. Its nature is out of the scope of this article. Here we only see it in action with nice animations, thanks to Popmotion.io.&lt;/p&gt;

&lt;p&gt;Here is the &lt;a href="https://pancakes-algorithm.herokuapp.com/"&gt;live demo&lt;/a&gt; you can play with. There are two text inputs and two buttons. In the first input, you enter the time interval&lt;br&gt;
which will be used for each animation round, i.e. how fast each pancake will be sorted. It's in milliseconds, which means if you enter the value 1000, the animation will execute for roughly 1 second. The second input is used to define how many pancakes you want to see sorting. The value there must be between 2 and 50. The buttons are self-explanatory enough. One is for starting the sorting animation, the other one is for resetting it. &lt;/p&gt;

&lt;p&gt;And &lt;a href="https://gitlab.com/mihailgaberov/pancake-algorithm-visualizer"&gt;here&lt;/a&gt; is where you can find the source code for the demo app. Feel free to check it out and take a closer look. You might try to amend the animations I did. I would be more than interested to see your versions. :)&lt;/p&gt;

&lt;p&gt;That was all. Nice and short, perfect for the summer! ☀️ 🏖&lt;/p&gt;

&lt;p&gt;🔥 Thanks for reading! 🔥&lt;/p&gt;

</description>
      <category>react</category>
      <category>algorithms</category>
      <category>popmotion</category>
    </item>
    <item>
      <title>How to optimize your JavaScript app by using service workers</title>
      <dc:creator>Mihail Gaberov</dc:creator>
      <pubDate>Tue, 03 Sep 2019 08:25:13 +0000</pubDate>
      <link>https://dev.to/mihailgaberov/how-to-optimize-your-javascript-app-by-using-service-workers-3imh</link>
      <guid>https://dev.to/mihailgaberov/how-to-optimize-your-javascript-app-by-using-service-workers-3imh</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5kTgbHTz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mihail-gaberov.eu/static/a30787b3e3507e66cf8239444bf0730f/a296c/optimize-your-javascript-service-workers.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5kTgbHTz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mihail-gaberov.eu/static/a30787b3e3507e66cf8239444bf0730f/a296c/optimize-your-javascript-service-workers.jpg" alt="Optimize your JavaScript app by using service workers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  A nice image used for demonstration of SW work.
&lt;/h6&gt;




&lt;p&gt;Every now and then we hear about optimizing something. There are different kinds of optimizations we could do to make our apps faster and more efficient. To save time or memory. This article will cover one of those methods — service workers.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;This tutorial explains what a service worker is and how to use it. In JavaScript. There is a code example at the end of it. If you want to skip the reading, &lt;a href="https://github.com/mihailgaberov/learn-service-workers"&gt;here&lt;/a&gt; is the Git repo and &lt;a href="https://compassionate-brahmagupta-71d9b4.netlify.com/"&gt;here&lt;/a&gt; you may see a live demo.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Theory
&lt;/h2&gt;

&lt;p&gt;Let’s see first what a worker is this 👀 and what service can we use it for 🤓.&lt;/p&gt;

&lt;p&gt;The service worker is a &lt;a href="https://developers.google.com/web/fundamentals/primers/service-workers/"&gt;simple script&lt;/a&gt;. JavaScript code, that your browser runs in the background, separate from a web page. It’s very convenient to use service workers for features that don’t need a web page or user interaction. One of the most common usages is intercepting and handling network requests. Which includes managing a cache of responses. The following is a simple example of how to include a service worker in your application run.&lt;/p&gt;

&lt;p&gt;Usually, in the entry point of your app, you put this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;serviceWorker&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
  &lt;span class="nb"&gt;window&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;load&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serviceWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/service-worker.js&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;This way of using service workers is a little bit improved than the basic one. Which would be directly calling the &lt;em&gt;register()&lt;/em&gt; method inside the &lt;em&gt;if statement&lt;/em&gt;. In this case, we use the window load event to register the service worker after the page has finished loading. After doing this, you need to add your service worker code in &lt;em&gt;service-worker.js&lt;/em&gt; file. At this point, you might want to take a look at mine service worker file here.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;All major browsers support Service Workers now and you can start using them right away.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Example
&lt;/h2&gt;

&lt;p&gt;Enough theory, let’s build a real example app, that will leverage the service workers feature.&lt;/p&gt;

&lt;p&gt;Let’s imagine we are building an app that needs to load a big chunk of data. It could be, for example, a nice, big full-screen image, we would display on the front page. Or it could be a big video clip we have to wait to load. This is an ideal use case for a service worker to shine. Let’s see how. 👀&lt;/p&gt;

&lt;p&gt;In our specific case, we will use the clock time to show the benefit of using service workers. What I mean is, that we will build a simple app, showing the time. It will have a nice, big button for fetching a nice, big image. And it will provide the user with an option to choose to use or not a service worker.&lt;/p&gt;

&lt;p&gt;Here is a screenshot of how it looks like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ba-oAlfh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mihail-gaberov.eu/static/af9d1550eecb5662e5706620049fe279/ac127/sw-demo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ba-oAlfh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mihail-gaberov.eu/static/af9d1550eecb5662e5706620049fe279/ac127/sw-demo.png" alt="Service Workers Demo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What this app demonstrates is, that when fetching the image (by clicking the button, wow!) with an active service worker — we don’t get blocked UI (user interface, i.e. fields, buttons, 🐛). If you choose not to use the service worker, you will get a frozen UI for a certain period of time. When the work completes and the main thread frees itself, it will unfreeze the UI.&lt;/p&gt;

&lt;p&gt;If you don’t want to clone and run the code yourself, jump straight to the &lt;a href="https://compassionate-brahmagupta-71d9b4.netlify.com/"&gt;live demo&lt;/a&gt;.&lt;/p&gt;

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

&lt;p&gt;This demo of service workers in action shows us the advantage we get from using them. Especially when you are trying to build responsive and robust JavaScript applications. No user wants to end up in a frozen page for an unknown time, as no developer should want that for his application’s users. Keeping in mind the above, service workers are a &lt;em&gt;must&lt;/em&gt; now. And we should not neglect them.&lt;/p&gt;

&lt;p&gt;🔥 Thanks for reading! 🔥&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>serviceworkers</category>
      <category>optimization</category>
    </item>
    <item>
      <title>How to build a chat app with React, TypeScript and Socket.io</title>
      <dc:creator>Mihail Gaberov</dc:creator>
      <pubDate>Tue, 30 Apr 2019 15:26:30 +0000</pubDate>
      <link>https://dev.to/mihailgaberov/how-to-build-a-chat-app-with-react-typescript-and-socket-io-2e1c</link>
      <guid>https://dev.to/mihailgaberov/how-to-build-a-chat-app-with-react-typescript-and-socket-io-2e1c</guid>
      <description>&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwxsfwbl9qs2z7a2r0jk8.jpeg" 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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwxsfwbl9qs2z7a2r0jk8.jpeg" alt="Build a chat app with React, TypeScript and Socketio"&gt;&lt;/a&gt;&lt;br&gt;
Photo by &lt;a href="https://unsplash.com/photos/3TRdlKU-3II?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Hutomo Abrianto&lt;/a&gt; on Unsplash&lt;/p&gt;

&lt;p&gt;Originally published at &lt;a href="https://mihail-gaberov.eu" rel="noopener noreferrer"&gt;mihail-gaberov.eu&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is going to be a thorough step-by-step guide for building a single page chat application using React, TypeScript and Socket.io.&lt;/p&gt;

&lt;p&gt;If you want to skip the reading, &lt;a href="https://github.com/mihailgaberov/chat" rel="noopener noreferrer"&gt;here&lt;/a&gt; 💁 is the GitHub repository with a detailed &lt;a href="https://github.com/mihailgaberov/chat/blob/master/README.md" rel="noopener noreferrer"&gt;README&lt;/a&gt; 🙌, and &lt;a href="https://mihails-chat.herokuapp.com/#/chat" rel="noopener noreferrer"&gt;here&lt;/a&gt; you can check the live demo. 🎀 In order to play with it, you need to open it in two different browsers (or browser tabs) or devices (you may use your computer and your smartphone) and chat with each other.🎀&lt;/p&gt;
&lt;h2&gt;
  
  
  Research
&lt;/h2&gt;

&lt;p&gt;When you are about to start a new project, it’s a good practice to do initial research about the technical stack you are planning to use.&lt;/p&gt;

&lt;p&gt;In other words, you may want or need — especially if you don’t have prior experience with it — to investigate on each technology you will be using. I recommend doing that separately. Take one of the them and create a small app that you can play with.&lt;/p&gt;

&lt;p&gt;If you need to check how the integration of two or more technologies is going to work in a real project — then you might want to include them all together in your “research-test-play” app — but preferably do your research one at a time.&lt;/p&gt;
&lt;h2&gt;
  
  
  Getting to the point
&lt;/h2&gt;

&lt;p&gt;When I started thinking about making this &lt;a href="http://mihails-chat.herokuapp.com/#/chat" rel="noopener noreferrer"&gt;chat application&lt;/a&gt;, I did exactly what I described above. I haven’t had recent experience with &lt;a href="http://www.typescriptlang.org/" rel="noopener noreferrer"&gt;TypeScript&lt;/a&gt; and none with &lt;a href="https://socket.io/" rel="noopener noreferrer"&gt;Socket.io&lt;/a&gt;, so I had to take a look at those and get myself familiarized with what is their current state. As my plan was to use &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;React&lt;/a&gt; as a main UI library, I needed to see how it was going to work with the other guys in the equation. So I did.&lt;/p&gt;

&lt;p&gt;I created two small applications (repos &lt;a href="https://github.com/mihailgaberov/playing-with-socketio" rel="noopener noreferrer"&gt;here&lt;/a&gt; and &lt;a href="https://github.com/mihailgaberov/react-contextapi-with-typescript" rel="noopener noreferrer"&gt;here&lt;/a&gt;) with these technologies, just to be able to play with them and learn how could I use them in my future chat application.&lt;/p&gt;

&lt;p&gt;After my initial research was done I was able to start thinking and planning the implementation of my main chat app.&lt;/p&gt;
&lt;h2&gt;
  
  
  High level planning
&lt;/h2&gt;

&lt;p&gt;Usually what people mean when they say “high level plan” is that they are looking for the big picture. Which means we need to create a rough plan of our execution and define our main pillars, but without going into too much detail. Now when we have clear idea of what to do, let’s start doing it! 👷&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: From this point forward I will be assuming that you are following my steps as I describe them, hence I will be writing in the second person. 👨&lt;/p&gt;
&lt;h3&gt;
  
  
  Tech stack
&lt;/h3&gt;

&lt;p&gt;We already mentioned the main technologies we will be using, but let’s define a proper list of all of them here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Microsoft/TypeScript-React-Starter#create-our-new-project" rel="noopener noreferrer"&gt;React with TypeScript&lt;/a&gt; (&lt;code&gt;create-react-app my-app --scripts-version=react-scripts-ts&lt;/code&gt;) — a UI library we will use for building our application’s user interfaces.&lt;br&gt;
&lt;a href="https://redux.js.org/" rel="noopener noreferrer"&gt;Redux&lt;/a&gt; — a state management library we will use for managing our application’s state.&lt;br&gt;
&lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;Express.js&lt;/a&gt; — Node.js web application framework we will use for creating an &lt;a href="https://expressjs.com/en/starter/hello-world.html" rel="noopener noreferrer"&gt;http server&lt;/a&gt; that we will need in our application, in order to take advantage of Socket.io engine.&lt;br&gt;
&lt;a href="https://socket.io/" rel="noopener noreferrer"&gt;Socket.io&lt;/a&gt; — a JavaScript library for real time web applications. It enables real time, bi-directional communication between web clients and servers. We will use it to implement a simple chat behavior in our app.&lt;br&gt;
&lt;a href="https://www.styled-components.com/docs" rel="noopener noreferrer"&gt;styled-components&lt;/a&gt; — a small library that we will be using for adding styles to our app and make the look and feel beautiful. It utilizes tagged template literals to style your components and removes the mapping between components and styles. This means that when you’re defining your styles, you’re actually creating a normal React component that has your styles attached to it.&lt;br&gt;
&lt;a href="https://jestjs.io/" rel="noopener noreferrer"&gt;Jest&lt;/a&gt;/&lt;a href="https://airbnb.io/enzyme/" rel="noopener noreferrer"&gt;Enzyme&lt;/a&gt; — a JavaScript Testing Framework and a JavaScript Testing Utility that we will be using to write unit tests for our application. Both have great integration into the React ecosystem and are heavily used in real projects.&lt;/p&gt;
&lt;h3&gt;
  
  
  Application Features
&lt;/h3&gt;

&lt;p&gt;In this section we will describe what the features of our application are going to be.&lt;/p&gt;

&lt;p&gt;Every time when we plan a new project, we must define certain criteria which will describe some level of completion when met.&lt;/p&gt;

&lt;p&gt;In other words, we need to set a limit point which, once reached, will show that our project is completed or at least in its first version. There is a famous saying, that could be matched to the issue with the “never ending” projects:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“A good plan today is better than a perfect plan tomorrow.” — or we may say that a working project today is better than a perfect project tomorrow.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is my list with the features I wanted to implement initially:&lt;/p&gt;
&lt;h3&gt;
  
  
  Header
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Chat tab — blinking when new message is received until it is read, or when the user is on Settings page&lt;/li&gt;
&lt;li&gt;Settings tab&lt;/li&gt;
&lt;li&gt;Unread messages counter&lt;/li&gt;
&lt;li&gt;Font Awesome icons&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Chat page
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Chat area (includes left aligned and right aligned messages)&lt;/li&gt;
&lt;li&gt;Message (text, datetime, left or right depending on if it’s received or sent)&lt;/li&gt;
&lt;li&gt;Showing the nickname only of the sender&lt;/li&gt;
&lt;li&gt;Message sender — input field and button. Input is cleared and focused when button is clicked&lt;/li&gt;
&lt;li&gt;Send messages with CTRL+ENTER&lt;/li&gt;
&lt;li&gt;Auto scroll to bottom when the chat area is not enough to show all messages&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Settings page
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;UserProfile component — possibility to change user name&lt;/li&gt;
&lt;li&gt;Interface color component — change the color theme of the app&lt;/li&gt;
&lt;li&gt;ClockDisplay component — change the time mode 12h or 24h, shown with each message&lt;/li&gt;
&lt;li&gt;Send messages with Ctrl+Enter — On/Off
-LanguageSwitcher — drop down menu allowing changing the language of the app (currently English and Deutsch are supported)&lt;/li&gt;
&lt;li&gt;Reset button — resets all settings stored to local storage&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Improvements
&lt;/h3&gt;

&lt;p&gt;At the time of writing this, there are still some pending features I would like to implement. Below is the list of all improvements I did or plan to do in future (the ones with the thumb emoji are already implemented):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add video chat feature.&lt;/li&gt;
&lt;li&gt;👍 Added AM/PM time formatting for when 12h mode is selected.&lt;/li&gt;
&lt;li&gt;👍 Added possibility to send message via ENTER by default. If the setting to send messages with CTRL+ENTER is ON, then this is going to be the only way (except via mouse/touch of course).&lt;/li&gt;
&lt;li&gt;👍 Optimized for iDevices (media queries).&lt;/li&gt;
&lt;li&gt;👍 Fix blinking/active class for the Chat tab issue — related to React Router not able to properly re-render connected components &lt;a href="https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/blocked-updates.md" rel="noopener noreferrer"&gt;https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/blocked-updates.md&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;👍 Clear input field when new message is sent.&lt;/li&gt;
&lt;li&gt;👍 Auto scroll to bottom main chat area when new messages exceed available space.&lt;/li&gt;
&lt;li&gt;👍 Prevent ‘doubling messages’ (or multiple messages duplicates when more clients are connected).&lt;/li&gt;
&lt;li&gt;👍 Add unit tests for the react components.
Add unit tests for redux stuff — reducers, store, action creators.&lt;/li&gt;
&lt;li&gt;👍 Add media queries for responsiveness — test and adjust on more devices.&lt;/li&gt;
&lt;li&gt;👍 Add demo to Heroku.&lt;/li&gt;
&lt;li&gt;👍 Add nice how-to in README.&lt;/li&gt;
&lt;li&gt;Add animations for the messages.&lt;/li&gt;
&lt;li&gt;Add sounds (with options to turn on/off in settings).&lt;/li&gt;
&lt;li&gt;Add more color themes.&lt;/li&gt;
&lt;li&gt;Add welcome message (broadcasts when a new user is connected).&lt;/li&gt;
&lt;li&gt;👍 Add icons (use Font Awesome).&lt;/li&gt;
&lt;li&gt;History of all the conversations.&lt;/li&gt;
&lt;li&gt;Handle case when socket’s connection state change (visually).&lt;/li&gt;
&lt;li&gt;Handle case when there has been a socket error.&lt;/li&gt;
&lt;li&gt;Handle case when a very long word (without) spaces is entered and it goes beyond the message background color.&lt;/li&gt;
&lt;li&gt;👍 Emoticons support — such as :D, :P, :), ;), 😉, ❤️, etc.&lt;/li&gt;
&lt;li&gt;👍 Link Parser — Youtube link (embedded video should appear), link to an image (embedded image should appear), all other links should appear as anchor.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When we know the initial plan and the requirements we need to fulfill, we can do our high level analyses. Our app will have two pages, Chat and Settings, accessible via tab controls.&lt;/p&gt;

&lt;p&gt;The Chat page will contain the main chat area with the controls needed to send messages (input field and a button).&lt;/p&gt;

&lt;p&gt;The Settings page will contain a few controls for selecting the options described above.&lt;/p&gt;

&lt;p&gt;With that in mind we can go to the next section where we will create a more detailed plan before the actual implementation.&lt;/p&gt;
&lt;h2&gt;
  
  
  More detailed planning
&lt;/h2&gt;

&lt;p&gt;In this section we need to have a deeper look at our application and define what will be the building blocks for it. Since we are going to use React and we know that in the React world the term component is widely used, we may refer to our building blocks as components. We will have components responsible for purely visual stuff, as well as such for managing the local storage, for example.&lt;/p&gt;

&lt;p&gt;Let’s try to imagine mentally how our app will look in the end and what components it will need. What we already know is this:&lt;/p&gt;
&lt;h3&gt;
  
  
  Server part
&lt;/h3&gt;

&lt;p&gt;We will need an HTTP server that will take care of starting the server and handling interactions with Socket.io (sending and receiving messages). Our server logic will be simple enough to live in only one file. You can see the actual implementation &lt;a href="https://github.com/mihailgaberov/chat/blob/master/server/index.js" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Client part
&lt;/h3&gt;

&lt;p&gt;Here we need to have all the visual controls, plus means for managing interactions with local storage, where we will save the user preferences, as well as handling of the translations and color themes.&lt;/p&gt;

&lt;p&gt;Now is a good moment to point out that for implementing the &lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/utilities/TranslationsProvider.tsx" rel="noopener noreferrer"&gt;translations and theming&lt;/a&gt; functionality in the app, I have used &lt;a href="https://reactjs.org/docs/context.html" rel="noopener noreferrer"&gt;React Context API&lt;/a&gt;. Also, since I knew I would have to deal with &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage" rel="noopener noreferrer"&gt;Local Storage&lt;/a&gt;, I did &lt;a href="https://github.com/mihailgaberov/misc/tree/master/manage-local-storage-with-typescript" rel="noopener noreferrer"&gt;another round&lt;/a&gt; of the “research-test-play” trip. And the output of it was that I already had a &lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/utilities/localStorageService.ts" rel="noopener noreferrer"&gt;nice service&lt;/a&gt;, which provides all the functionalities I needed.&lt;/p&gt;

&lt;p&gt;Another thing you will notice when looking at the &lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components" rel="noopener noreferrer"&gt;components&lt;/a&gt; folder is that every component has its own directory with a few files in it.&lt;/p&gt;

&lt;p&gt;These files serve the following logic:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;index.ts&lt;/strong&gt; → entry point, just expose the component itself. This helps for not having to write repeatedly and long import statements. Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Instead of having to write this:&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ChatArea&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;../../ChatArea/ChatArea&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// We can have just this:&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ChatArea&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;../../ChatArea&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;&lt;strong&gt;ComponentName.tsx&lt;/strong&gt; (&lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/components/ChatArea/ChatArea.tsx" rel="noopener noreferrer"&gt;ChatArea.tsx&lt;/a&gt;) → actual component implementation live here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ComponentName.test.tsx&lt;/strong&gt; (ChatArea.test.tsx) → unit tests of the component live here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;StyledComponentName.tsx&lt;/strong&gt; (StyledChatArea.tsx) → CSS styles of the component live here.&lt;/p&gt;

&lt;p&gt;The same pattern is used for most of the components, exception are only the &lt;em&gt;pages&lt;/em&gt;, such as the components which play the role of parents for all the inner parts — &lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/pages/Chat" rel="noopener noreferrer"&gt;ChatPage&lt;/a&gt; and &lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/pages/Settings" rel="noopener noreferrer"&gt;SettingsPage&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So, with that said, I think we can see what would be our application structure when we try to “componentize” it. Here a list of the &lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components" rel="noopener noreferrer"&gt;components&lt;/a&gt; I came up with:&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ANY35awws7ZOkh8etHOJkjw.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ANY35awws7ZOkh8etHOJkjw.png" alt="Chat application components"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: all names are a matter of personal choice, feel free to name yours as you wish.&lt;/p&gt;

&lt;p&gt;Let me try to give you a bit more detailed explanation for each of them below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/AppRouter" rel="noopener noreferrer"&gt;AppRouter&lt;/a&gt; — contains the main app routing logic. For instance, here we define the app routes by giving them the path and component to load when this path is reached. Uses &lt;a href="https://reacttraining.com/react-router/web/guides/philosophy" rel="noopener noreferrer"&gt;React Router&lt;/a&gt; package.&lt;br&gt;
&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/ChatArea" rel="noopener noreferrer"&gt;ChatArea&lt;/a&gt; — represents the main chat area, where all the messages are being displayed. It’s responsible also for auto scrolling down when the visible area limit is reached.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ASx6WIoqTnxRZ4YO3egA6BA.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ASx6WIoqTnxRZ4YO3egA6BA.png" alt="ChatArea component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/ClockModeSelector" rel="noopener noreferrer"&gt;ClockModeSelector&lt;/a&gt; — responsible for displaying controls allowing the user to select the time display mode -12h or 24h. It uses a common component called &lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/common/RadioGroup" rel="noopener noreferrer"&gt;RadioGroup&lt;/a&gt; (will describe it below) and the Local Storage service to write/read from the browser’s local storage.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2A53qZzpKfO6ALW5So93X6dQ.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2A53qZzpKfO6ALW5So93X6dQ.png" alt="ClockModeSelector component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/common/RadioGroup" rel="noopener noreferrer"&gt;common/RadioGroup&lt;/a&gt; — this is a common component, built with the idea to be re-usable all over the app. We use this component in a few other components, such as ClockModeSelector, &lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/ThemeSelector" rel="noopener noreferrer"&gt;ThemeSelector&lt;/a&gt; and &lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/SendingOptions" rel="noopener noreferrer"&gt;SendingOptions&lt;/a&gt;. It contains logic for displaying two radio buttons with the possibility to pass a callback function which will execute a certain action depending on your needs.&lt;br&gt;
&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/LanguageSelector" rel="noopener noreferrer"&gt;LanguageSelector&lt;/a&gt; — responsible for displaying a select input control for choosing the app language. It accepts a function that is coming from the &lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/utilities/TranslationsProvider.tsx" rel="noopener noreferrer"&gt;TranslationsProvider&lt;/a&gt; utility and does the actual language change.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AudPi2SgeogBN5QL_q23Hpg.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AudPi2SgeogBN5QL_q23Hpg.png" alt="LanguageSelector component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/Message" rel="noopener noreferrer"&gt;Message&lt;/a&gt; — this component is responsible for displaying each chat message, sent or received. It includes the nickname of the sender and timestamp showing the time when the message was sent/received. It also provides support for emojis (like ❤️) and links parsing — see the screenshot 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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AGtM3k_EoB2Iq-Wya5p_s2g.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AGtM3k_EoB2Iq-Wya5p_s2g.png" alt="Message component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/MessageSender" rel="noopener noreferrer"&gt;MessageSender &lt;/a&gt;— this is the component that provides the necessary user interface controls for sending messages — a text input field and a Send button. It contains logic for defining the different ways of sending — via click or key press (with ENTER or CTRL+ENTER), as well as clearing the input field when a new message is sent.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AXogXNNyuyEGNeg6JzKlktg.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AXogXNNyuyEGNeg6JzKlktg.png" alt="MessageSender component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/Navigation" rel="noopener noreferrer"&gt;Navigation&lt;/a&gt; — here lives the implementation of the app navigation. It consists of two tabs — &lt;strong&gt;Chat&lt;/strong&gt; and &lt;strong&gt;Settings&lt;/strong&gt; and contains logic for connecting to sockets, by sending a &lt;a href="https://redux.js.org/basics/actions" rel="noopener noreferrer"&gt;Redux action&lt;/a&gt; when the component is mounted. It manages an &lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/UnreadMessagesCounter" rel="noopener noreferrer"&gt;UnreadMessagesCounter&lt;/a&gt; component by passing it the count of the currently unread messages (this happens when the user receives a message while being on the Settings page). It also has a logic responsible for making the tab blink when a new message arrives.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ANl2-3zJvidqV8DG4j4dddQ.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ANl2-3zJvidqV8DG4j4dddQ.png" alt="Navigation component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/components/Nickname/" rel="noopener noreferrer"&gt;Nickname &lt;/a&gt;— this is simple component for rendering the nickname of a chat user.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AIXnA_J1kUyMCY3lc40vK0Q.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AIXnA_J1kUyMCY3lc40vK0Q.png" alt="Nickname component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/ResetButton" rel="noopener noreferrer"&gt;ResetButton&lt;/a&gt; — this will be a simple component, used in the Settings page for rendering a Reset button. The function is going to be exactly that — resetting the settings selections that are already saved into the local storage, if any.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2A47R5gBNjjBThsC_SYWfYlA.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2A47R5gBNjjBThsC_SYWfYlA.png" alt="ResetButton component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/SendingOptions" rel="noopener noreferrer"&gt;SendingOptions&lt;/a&gt; — responsible for displaying options for choosing if a message can be sent via CTRL+ENTER. It works same way as ClockModeSelector component — uses RadioGroup component and accepts a callback function.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AbxAP64H75j0BuMzrpGAaow.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AbxAP64H75j0BuMzrpGAaow.png" alt="SendingOptions component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/ThemeSelector" rel="noopener noreferrer"&gt;ThemeSelector&lt;/a&gt; — same as the component above. The only difference is that here the user is allowed to select a color theme. In our case the options are only two — light theme or dark theme.&lt;/p&gt;

&lt;p&gt;ThemeSelector component&lt;br&gt;
&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/Timestamp" rel="noopener noreferrer"&gt;Timestamp &lt;/a&gt;— simple component used for rendering the time of the messages.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AFm-vlKR0ROEBsNnOKhgieA.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AFm-vlKR0ROEBsNnOKhgieA.png" alt="Timestamp component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/UnreadMessagesCounter" rel="noopener noreferrer"&gt;UnreadMessagesCounter&lt;/a&gt; — this is the component I mentioned a little bit earlier. It shows a counter indicating the number of the received, but not yet read, messages. It’s positioned in the navigation area.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AjZecIylkAB0s1h-Z4bjKAA.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AjZecIylkAB0s1h-Z4bjKAA.png" alt="UnreadMessagesCounter component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/UserProfile" rel="noopener noreferrer"&gt;UserProfile&lt;/a&gt; — this the component responsible for rendering an input field the user can use for changing its user name. It’s saving the new user name into the local storage, using a &lt;a href="https://lodash.com/docs/4.17.11#debounce" rel="noopener noreferrer"&gt;debounce&lt;/a&gt; function. This means that the actual triggering of the function is happening some defined time after the user stops typing. It also triggers another Redux action, so we can use the new username in our Redux state.&lt;br&gt;
&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/pages/Chat" rel="noopener noreferrer"&gt;pages/ChatPage&lt;/a&gt;— parent component that encloses everything shown on the Chat page.&lt;br&gt;
&lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/components/pages/Settings" rel="noopener noreferrer"&gt;pages/SettingsPage&lt;/a&gt; — parent component that encloses everything shown on the Settings page.&lt;br&gt;
Everything described above was related to our React components. All of them are responsible for getting some data and displaying it in a proper way. In order to be able to handle this data in a convenient for us way, we use a few more things. Let’s have a look at these things in the sections below.&lt;/p&gt;
&lt;h2&gt;
  
  
  Redux State Management
&lt;/h2&gt;

&lt;p&gt;Here we will talk about how our app state is being managed by using Redux and socket middleware.&lt;/p&gt;
&lt;h3&gt;
  
  
  Store
&lt;/h3&gt;

&lt;p&gt;Our &lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/store/index.ts" rel="noopener noreferrer"&gt;store&lt;/a&gt; is going to be relatively simple. We will have only two reducers defining a piece of the state reserved for the socket state and for the messages state. This is also where we apply our middleware. If you are familiar with &lt;a href="https://redux-saga.js.org/" rel="noopener noreferrer"&gt;Redux Saga&lt;/a&gt; package, you have probably seen this pattern of applying custom middleware when using Redux.&lt;/p&gt;

&lt;p&gt;Something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createStore&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;applyMiddleware&lt;/span&gt; &lt;span class="p"&gt;}&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;redux&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;createSagaMiddleware&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;redux-saga&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;reducer&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;./reducers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;mySaga&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;./sagas&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="c1"&gt;// create the saga middleware&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sagaMiddleware&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createSagaMiddleware&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// mount it on the Store&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;applyMiddleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sagaMiddleware&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;But in our case it would be like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createStore&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;combineReducers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;applyMiddleware&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="p"&gt;}&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;redux&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;socketReducer&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;./socket/reducer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;messageReducer&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;./message/reducer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;socketMiddleware&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;./socket/middleware&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rootReducer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combineReducers&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;socketState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;socketReducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;messageState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;messageReducer&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="c1"&gt;// @ts-ignore&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;composeEnhancers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__REDUX_DEVTOOLS_EXTENSION_COMPOSE__&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;compose&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;index&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="nf"&gt;createStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rootReducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;composeEnhancers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;applyMiddleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;socketMiddleware&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;index&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Message
&lt;/h3&gt;

&lt;p&gt;After defining our store, it’s time to see how are we going to handle the messaging in Redux. We have defined our actions &lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/store/message/actions/index.ts" rel="noopener noreferrer"&gt;here&lt;/a&gt; and our messageReducer &lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/store/message/reducer/index.ts" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actions&lt;/strong&gt; — here we define the main actions needed for sending and receiving messages, as well as changing the user name.&lt;br&gt;
&lt;strong&gt;Reducer&lt;/strong&gt; — here is where our messageReducer function lives and where we define what happens when one of the actions above is dispatched.&lt;/p&gt;
&lt;h2&gt;
  
  
  Socket
&lt;/h2&gt;

&lt;p&gt;We follow the same logic as above here. We have our &lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/store/socket/actions/index.ts" rel="noopener noreferrer"&gt;socket actions&lt;/a&gt;, the &lt;a href="https://github.com/mihailgaberov/chat/tree/master/src/store/socket/middleware" rel="noopener noreferrer"&gt;middleware&lt;/a&gt; I mentioned above, and the &lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/store/socket/reducer/index.ts" rel="noopener noreferrer"&gt;socketReducer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actions&lt;/strong&gt; — contains actions for connecting the socket (the one dispatched from the Navigation component in the beginning when the application is started) and one for when the connection status is changed, i.e. showing if we are connected or not.&lt;br&gt;
&lt;strong&gt;Middleware&lt;/strong&gt; — contains implementation of a simple socket middleware, which provides us with the minimum functionality we need in our chat app.&lt;br&gt;
&lt;strong&gt;Reducer&lt;/strong&gt; — here is where our socketReducer function lives and where we define what happens when one of the actions above is dispatched.&lt;/p&gt;
&lt;h2&gt;
  
  
  Theming
&lt;/h2&gt;

&lt;p&gt;In order to implement the possibility for setting different color themes in our application and considering the fact we are using styled-components, I used a &lt;a href="https://www.styled-components.com/docs/advanced" rel="noopener noreferrer"&gt;ThemeProvider&lt;/a&gt; — component provided by them. &lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/theme/index.ts" rel="noopener noreferrer"&gt;Here&lt;/a&gt; is the implementation that includes defining objects with custom colors used in the themes.&lt;/p&gt;

&lt;p&gt;The logic behind applying the selected color theme &lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/utilities/TranslationsProvider.tsx" rel="noopener noreferrer"&gt;resides&lt;/a&gt; here. Ideally the containing component should be named something different than &lt;em&gt;TranslationsProvider&lt;/em&gt;, as it doesn’t handle only the translations, as we see. We could add this to the list of future improvements/refactoring.&lt;/p&gt;

&lt;p&gt;Here is how the existing color themes look:&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AtooJRolBzH403D6jMQUVLg.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AtooJRolBzH403D6jMQUVLg.png" alt="theming"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Utilities
&lt;/h2&gt;

&lt;p&gt;In almost every software project, at certain point, the need of common reusable functions emerges. This is the moment when developers usually create a common shared file or files, containing such helpers functions. In our case this would be &lt;strong&gt;/utilities&lt;/strong&gt; folder that currently contains four files. I will go through each of them below and explain the logic behind my decision to create it and put it there:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/utilities/common.ts" rel="noopener noreferrer"&gt;common.ts&lt;/a&gt; — here is the place where I decide to put such common helper functions, which are supposed to be easily used where needed in the whole application. In this specific case you will find four functions used for time formatting, and a helper for defining the active page and for scrolling an element to bottom.&lt;br&gt;
&lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/utilities/localStorageService.ts" rel="noopener noreferrer"&gt;localStorageService.ts&lt;/a&gt; — I have already mention this service in the first part of this tutorial. Here is where all methods for manipulating the local storage live.&lt;br&gt;
&lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/utilities/TranslationsProvider.tsx" rel="noopener noreferrer"&gt;TranslationsProvider.tsx&lt;/a&gt; — this component was also mentioned multiple times, but for the sake of clarity I will mention it again here. It contains the logic for providing translations and color theme in the app.&lt;br&gt;
&lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/utilities/withTranslations.tsx" rel="noopener noreferrer"&gt;withTranslations.tsx&lt;/a&gt; — this is a &lt;a href="https://tylermcginnis.com/react-higher-order-components/" rel="noopener noreferrer"&gt;higher-order component (HOC)&lt;/a&gt; which is responsible for attaching the application context (containing the translations and themes themselves) to any component being wrapped by it.&lt;br&gt;
Here is an example of how is it used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;withTranslations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;SettingsPage&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FunctionComponent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have walked a long way to here and we still haven’t started with the actual implementation.&lt;/p&gt;

&lt;p&gt;That is a vivid pointer for us to show how important and extensive the planning phase of a project could be.&lt;/p&gt;

&lt;p&gt;Let’s jump now to the implementation phase in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;If you reached this point of the tutorial, you should have a very clear idea of what are we going to build. Here, we are about to find out how are we going to do it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Starting small
&lt;/h3&gt;

&lt;p&gt;As with any other project we should strive to start with small, incremental chunks and build on them. In our case I have decided to start first with building the header navigation. The reason for that was that I wanted to have the router and the navigation controls in place, so I could easily navigate through the tabs while developing and testing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Settings page
&lt;/h3&gt;

&lt;p&gt;After I had finished with the header and navigation parts, I decided to jump to the settings page first. Again, my reasoning was very simple — I wanted to build first what I was going to use in the Chat page. In other words I wanted to be able to customize my chat area, messages, ways of sending and so on, before implementing them.&lt;/p&gt;

&lt;p&gt;So I started building component by component as I described them in the previous section. Once I had the full Settings page finished, I was able to go and start implementing the Chat page components. But before that, I had to take care of the supporting stuff — integrating with &lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/utilities/localStorageService.ts" rel="noopener noreferrer"&gt;local storage&lt;/a&gt; and adding &lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/utilities/TranslationsProvider.tsx" rel="noopener noreferrer"&gt;translations mechanism&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Chat page
&lt;/h3&gt;

&lt;p&gt;After I have done all from above, the implementation of the Chat page and its components was fairly easy. I had to take care of the visual part manly and make the integration with the Redux store. As you &lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/components/pages/Chat/ChatPage.tsx" rel="noopener noreferrer"&gt;already saw&lt;/a&gt;, I had to implement only two components which are shown and used on the Chat page — ChatArea and MessageSender.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding improvements
&lt;/h2&gt;

&lt;p&gt;I want to say a few words here regarding the app improvements we did or will do in the future. Usually when we have a new requirement (let’s call it “requirement”, that makes is sound closer to what would be in a real project), it is again a very good idea to do some initial research, instead of jumping directly into implementation. You will be surprised how many solutions are already out there, waiting for us to use them.&lt;/p&gt;

&lt;p&gt;In other words, we don’t have to invent the wheel again.&lt;/p&gt;

&lt;p&gt;This is what I did when I started thinking about adding support for emoticons or link parsing. It turned out that there are already solutions I could use with a little tweaking from my side, just to make them fit well in my project.&lt;/p&gt;

&lt;p&gt;Here are the links to the packages I used:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/linkifyjs" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/linkifyjs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.microlink.io/sdk/getting-started/react/" rel="noopener noreferrer"&gt;https://docs.microlink.io/sdk/getting-started/react/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/react-emojione" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/react-emojione&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/get-urls" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/get-urls&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And &lt;a href="https://github.com/mihailgaberov/chat/blob/master/src/components/Message/Message.tsx" rel="noopener noreferrer"&gt;here&lt;/a&gt; you can see how I used them in our chat app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploying to Heroku
&lt;/h2&gt;

&lt;p&gt;I have written &lt;a href="https://mihail-gaberov.eu/creating-twitter-bot/" rel="noopener noreferrer"&gt;another article&lt;/a&gt; in the past. It was about totally different subject, but there is a part exactly related to how to deploy an app to Heroku. You might find it useful to check it out.&lt;/p&gt;

&lt;p&gt;For deploying our chat application to &lt;a href="https://herokuapp.com/" rel="noopener noreferrer"&gt;Heroku&lt;/a&gt;, I will assume you have already an account and can easily follow the steps below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;npm build&lt;/code&gt; to build the project to &lt;code&gt;build&lt;/code&gt; folder.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;build&lt;/code&gt; folder to Git to make sure it will be committed.&lt;/li&gt;
&lt;li&gt;Make sure that express server loads static resources from it.&lt;/li&gt;
&lt;li&gt;Commit all: &lt;code&gt;git commit -m 'Deploy to Heroky'.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;git push heroku master&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Open the app from the given URL (in my case: &lt;a href="https://mihails-chat.herokuapp.com/#/chat" rel="noopener noreferrer"&gt;mihails-chat.herokuapp.com&lt;/a&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Future (possible) plans
&lt;/h2&gt;

&lt;p&gt;At the time of writing this I was thinking it might be very interesting to try building the same application with the other super famous UI library on the market — &lt;a href="https://angular.io/" rel="noopener noreferrer"&gt;Angular&lt;/a&gt;. I still think it will be worth it, but I am not sure whether I will have the time and the power to do it 😐.&lt;/p&gt;

&lt;p&gt;In any case, what I am thinking about it as a pure, technical comparison of two major UI libraries from the developer’s point of view.&lt;/p&gt;

&lt;p&gt;Another possible extension would be to add video chat functionality using WebRCT or similar technology.&lt;/p&gt;

&lt;p&gt;If I do it, I will make sure you know it!&lt;/p&gt;

&lt;p&gt;🔥 Thanks for reading! 🔥&lt;/p&gt;

</description>
      <category>chat</category>
      <category>react</category>
      <category>typescript</category>
      <category>socketio</category>
    </item>
    <item>
      <title>What should we test (ReactJS Components)</title>
      <dc:creator>Mihail Gaberov</dc:creator>
      <pubDate>Thu, 25 Apr 2019 12:06:32 +0000</pubDate>
      <link>https://dev.to/mihailgaberov/what-should-we-test-reactjs-components-9hp</link>
      <guid>https://dev.to/mihailgaberov/what-should-we-test-reactjs-components-9hp</guid>
      <description>&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%2Fgithub.com%2Fmihailgaberov%2Fmihail-gaberov.eu%2Fraw%2Fmaster%2Fsrc%2Fpages%2Fwhat-should-we-test-reactjs%2Fwhat-should-we-test-head.jpeg" 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%2Fgithub.com%2Fmihailgaberov%2Fmihail-gaberov.eu%2Fraw%2Fmaster%2Fsrc%2Fpages%2Fwhat-should-we-test-reactjs%2Fwhat-should-we-test-head.jpeg" alt="What should we test ReactJS Components"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Photo by &lt;a href="https://unsplash.com/photos/ZMraoOybTLQ?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Artem Sapegin&lt;/a&gt; on &lt;a href="https://unsplash.com/search/photos/unit-testing-reactjs?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;
&lt;/h6&gt;

&lt;p&gt;There are a lot of discussions in the web when we talk about testing. And even more about unit testing. In &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;React&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is going to be a brief summary of what and how should we think about when we test a &lt;a href="https://github.com/facebook/react/" rel="noopener noreferrer"&gt;ReactJS&lt;/a&gt; application. More specific, what should we write tests for, when creating React components for our app. If you want to skip the reading, &lt;a href="http://mihailgaberov.github.io/testing-reactjs-presentation/" rel="noopener noreferrer"&gt;here&lt;/a&gt; you can find shorter version in a few slides.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;All of the points below are recommended by people smarter than me. I just collected them as a summary of best practices, trying to help my team to decide on the matter. And start using them in our day-to-day work.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Do we need unit tests?&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%2Fgithub.com%2Fmihailgaberov%2Fmihail-gaberov.eu%2Fraw%2Fmaster%2Fsrc%2Fpages%2Fwhat-should-we-test-reactjs%2F1.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%2Fgithub.com%2Fmihailgaberov%2Fmihail-gaberov.eu%2Fraw%2Fmaster%2Fsrc%2Fpages%2Fwhat-should-we-test-reactjs%2F1.png" alt="Do we need unit tests?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a long time discussion, that has only one right answer. Yes! Tests provide developers with confidence. This confidence allows us to produce software with better quality. To do proper refactoring. And to reason better about what we do.&lt;/p&gt;

&lt;p&gt;In short term it might look it’s not worth it. That it takes too much time. Time we might spend for implementing features instead. But very soon after the start of every project, the payoff of having a nice test suit is huge. The punishment of not having such, especially when a release date is approaching — disaster!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test render logic&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we write tests for our components, we write them in such way that they test only the render logic and don’t care about any internal implementation. What does this mean? It means that, your test should be testing the things that the user will be seeing and interacting with. Let’s say you are testing a component that displays a button on the screen.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;When the user clicks the button, your component renders a message in a div element. Your test should check practically for the same — check if the button is clickable and if this div exists after the clicking.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;No lifecycle methods&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Tests should not test lifecycle methods, they are supposed to be covered by React itself. If we have some logic that needs to be tested in these methods, we should try to extract it in another testable place and only use it in there. Ideally, in our &lt;em&gt;componentDidMount&lt;/em&gt; method for example, we should have only functionality for fetching data (calling an API for example). All the rest that you might be tempted to leave there, should be extracted in other methods, that could be tested, if needed.&lt;/p&gt;

&lt;p&gt;No outsiders&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;(Unit) Tests should not test interaction with outside world!&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The name “unit” implies independence and autonomy. A unit test should be an encapsulated piece of code, that has inside everything it needs to perform its job. When we need to write tests which need data, we use mocks. That said, every interaction with APIs, DBs, etc., is not a matter of unit tests and should not be tested in such.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Small, smaller, easier&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Tests should be small and easy to read — if we need to test a big component with a lot of dependencies, we need to consider splitting it to smaller testable pieces.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Dīvide et imperā.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As the famous quote above implies, it’s a lot more easier to deal with small, well modularized components, than the opposite. Also, if we need to use big mocks, we need to extract them out of the test and only use them by importing, in order to avoid polluting the test file.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Given a simple component containing a button and a text field&lt;/li&gt;
&lt;li&gt;We should test what the user sees — rendering, and what the user can do — user interaction&lt;/li&gt;
&lt;li&gt;If there is visual change after clicking — test for it&lt;/li&gt;
&lt;li&gt;If there is a value returned after clicking — test for it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;But we don’t care about the internal implementation of the click handler!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I have prepared a small example, trying to demonstrate the above. You can find it &lt;a href="https://github.com/mihailgaberov/testing-reactjs-examples" rel="noopener noreferrer"&gt;here&lt;/a&gt;. In order to run it locally on your machines, just clone it and install the dependencies. Then do &lt;em&gt;‘npm run test’&lt;/em&gt;. 🚀 📜&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are many resources on that subject online, but I am going to list here the ones I have found very useful and learned from them.&lt;/p&gt;

&lt;p&gt;👏 A &lt;a href="https://medium.freecodecamp.org/the-right-way-to-test-react-components-548a4736ab22" rel="noopener noreferrer"&gt;nice article&lt;/a&gt; on the subject&lt;/p&gt;

&lt;p&gt;👏 &lt;a href="https://www.leighhalliday.com/testing-react-jest-enzyme-sinon" rel="noopener noreferrer"&gt;Another good one&lt;/a&gt; for testing with Jest, Enzyme and Sinon&lt;/p&gt;

&lt;p&gt;👏 &lt;a href="https://github.com/sapegin/jest-cheat-sheet" rel="noopener noreferrer"&gt;Jest Cheat sheet&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👏 &lt;a href="https://mihailgaberov.github.io/testing-reactjs-presentation" rel="noopener noreferrer"&gt;Presentation slides&lt;/a&gt; online&lt;/p&gt;

&lt;p&gt;👏 &lt;a href="https://github.com/mihailgaberov/testing-reactjs-examples" rel="noopener noreferrer"&gt;Github repo&lt;/a&gt; with working examples&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The important lesson we must keep from all this testing stuff is, that no matter how exactly we write our tests (well it matters…a bit 💬), as long as we write tests at all. This way of thinking and working should become our second nature when we are striving to produce errorproof software. With time and practice the things become better and easier. The most important thing for us is to never stop improving.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Keep going!&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;🔥 Thanks for reading! 🔥&lt;/p&gt;

</description>
      <category>react</category>
      <category>unittesting</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to solve the Sherlock and Anagrams coding challenge in JavaScript</title>
      <dc:creator>Mihail Gaberov</dc:creator>
      <pubDate>Wed, 24 Apr 2019 07:39:47 +0000</pubDate>
      <link>https://dev.to/mihailgaberov/how-to-solve-the-sherlock-and-anagrams-coding-challenge-in-javascript-dpk</link>
      <guid>https://dev.to/mihailgaberov/how-to-solve-the-sherlock-and-anagrams-coding-challenge-in-javascript-dpk</guid>
      <description>

&lt;p&gt;Originally published at &lt;a href="https://mihail-gaberov.eu"&gt;mihail-gaberov.eu&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LOnWQfNg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/mihailgaberov/mihail-gaberov.eu/raw/master/src/pages/sherlock-and-anagrams/sherlock-and-anagrams.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LOnWQfNg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/mihailgaberov/mihail-gaberov.eu/raw/master/src/pages/sherlock-and-anagrams/sherlock-and-anagrams.jpg" alt='Solve "Sherlock and Anagrams" coding challenge in JavaScript'&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Photo by &lt;a href="https://unsplash.com/photos/qYfwGVNJqSA?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Javier Quesada&lt;/a&gt; on &lt;a href="https://unsplash.com/search/photos/task-anagrams?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;
&lt;/h6&gt;




&lt;p&gt;This post is going to get you through my solution of a coding challenge called "Sherlock and Anagrams". You may take a look at it in &lt;a href="https://www.hackerrank.com/challenges/sherlock-and-anagrams/"&gt;HackerRank&lt;/a&gt;. I spent a lot of time trying to solve it, with JavaScript. When I tried to google it, I could not find a decent JS solution. I found just one and it was not working correctly. Also any explanations were completely out of the question. That's why I decided to write an article about it and try to put some nice and easy to digest explanations along the way. Keep reading now!&lt;/p&gt;

&lt;p&gt;⚠ CAUTION: &lt;em&gt;I will roll out my solution below with short explanations about each of the steps. If you want to give a try yourself, please stop here and go to HackerRank site&lt;/em&gt;.&lt;/p&gt;

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

&lt;p&gt;Two strings are anagrams of each other if the letters of one string can be rearranged to form the other string. Given a string, find the number of pairs of substrings of the string that are anagrams of each other.&lt;/p&gt;

&lt;p&gt;For example s = mom, the list of all anagrammatic pairs is [m, m], [mo, om] at positions [[0], [2]], [[0, 1], [1, 2]] respectively.&lt;/p&gt;

&lt;h4&gt;
  
  
  Constraints
&lt;/h4&gt;

&lt;p&gt;Length of the input string: 2 ≤ |s| ≤ 100&lt;/p&gt;

&lt;p&gt;String s contains only lowercase letters from the range ascii[a-z].&lt;/p&gt;




&lt;h2&gt;
  
  
  Analysis
&lt;/h2&gt;

&lt;p&gt;First thing first - we need to get a better understanding of the whole problem. What is an anagram? What is an anagrammatic pair? Can I see one? Also, what exactly does it mean substrings?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;In other words, we need to have a clear picture of what are we trying to solve, before solving it.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From the description of the problem we can deduct all we need. Keep walking! 🚶&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I think here is a good moment to mention that the challenge in question is under "Dictionaries and Hashmaps" section in HackerRank website, so it's very likely for one to think, that probably he has to use this kind of data structures when solving it.&lt;/em&gt; 😇&lt;/p&gt;

&lt;h3&gt;
  
  
  Anagrams
&lt;/h3&gt;

&lt;p&gt;Since we are going to look for anagrams, let's start with them. As it is described above, an anagram of one word is another word, that has the same length and is created with the same characters from the former one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6OZfJVzB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://github.com/mihailgaberov/mihail-gaberov.eu/raw/master/src/pages/sherlock-and-anagrams/anagram-listen-silent.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6OZfJVzB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://github.com/mihailgaberov/mihail-gaberov.eu/raw/master/src/pages/sherlock-and-anagrams/anagram-listen-silent.gif" alt='Animation for the anagram "Listen = Silent"'&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So we will have to look for words and compare them with other words, in order to see if they are anagrammatic pairs. Once found, we will just count them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Anagrammatic pairs
&lt;/h3&gt;

&lt;p&gt;After we saw what an anagram is, it should be relatively easy to conclude, that anagrammatic pair is just two strings that are anagrams. Such as "mo" and "om", or "listen" and "silent". We will have to count how many pairs like this could be found in a given string. In order to do that, we need to split this original string to substrings.&lt;/p&gt;

&lt;h3&gt;
  
  
  Substrings
&lt;/h3&gt;

&lt;p&gt;Substrings, as the name infer, are parts of a string. These parts could be just a letter or a pair of letters, such as what have we seen in the example above - "m" or "mo". In our solution we will split the original string to such substrings and then we will go over them and do the comparison, which will tell us whether we have anagrammatic pairs among them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;Now, when we did our analysis, it's showtime! 🎆&lt;/p&gt;

&lt;p&gt;Let's summarize:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We need to find all substrings of the given string - create a method for that.&lt;/li&gt;
&lt;li&gt;We need to be able to check if two strings are anagrams - create a method for that.&lt;/li&gt;
&lt;li&gt;We need to count all anagrammatic pairs in the given string - create a method for that.&lt;/li&gt;
&lt;li&gt;Combine everything from above and spit the result - create a method for that.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Get all substrings
&lt;/h3&gt;

&lt;p&gt;This would be our helper method for finding all substring of a given string:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getAllSubstrings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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="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="nx"&gt;result&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="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;str&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="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;str&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;+&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;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&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="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&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, it has O(n&lt;sup&gt;2)&lt;/sup&gt; time complexity, but for our case it does the job, because we have limited length of the input string (up to 100 characters).&lt;/p&gt;

&lt;h3&gt;
  
  
  Check for anagrams
&lt;/h3&gt;

&lt;p&gt;This would be our helper method for checking if two strings are anagrammatic pair:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;isAnagram&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;str2&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;hist&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;str1&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="kd"&gt;const&lt;/span&gt; &lt;span class="kr"&gt;char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str1&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hist&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;char&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;hist&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;char&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;hist&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;char&lt;/span&gt;&lt;span class="p"&gt;]&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="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="mi"&gt;0&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;str2&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="kd"&gt;const&lt;/span&gt; &lt;span class="kr"&gt;char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str2&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hist&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;char&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;hist&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;char&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;❕ You remember, judging by the section where this challenge belongs, we assumed that most probably we will have to use data structures such as hashmaps or dictionaries. Here is the moment to notice that. We use a simple JavaScript object to play the role of a hashmap. We do two iterations - one per string. When we iterate over the first one, we add its characters as keys to the hashmap and count their appearances, which are going to be stored as their values. Then we do another iteration over the second string. Check if its characters are stored in our hashmap. If yes - decrement their value. If there are missing characters, which means the two strings are not anagrammatic pair, we simply return false. If both loops complete, we return true, signifying that the strings being analysed are anagrammatic pair.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do the counting
&lt;/h3&gt;

&lt;p&gt;This is the method, where we will use the helper for checking if a pair is anagrammatic and count it. We do that with the help of JavaScript arrays and the methods they provide. We iterate over an array containing all the substrings of the original string. Then we get the currect element and remove it from the array. And then we do another loop through that array and return 1 if we find that there is an anagram of the current element. If nothing is found, we return 0.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;countAnagrams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentIndex&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;currentElement&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;currentIndex&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;arrRest&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;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentIndex&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="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;arrRest&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentElement&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;===&lt;/span&gt; &lt;span class="nx"&gt;arrRest&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="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;isAnagram&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentElement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arrRest&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="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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  And in the end
&lt;/h3&gt;

&lt;p&gt;The only thing left to be done now is to combine all of the above and spit the desired result. Here is how the final method looks like:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sherlockAndAnagrams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&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;duplicatesCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;v&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;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;length&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;duplicatesCount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;anagramsCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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="nx"&gt;getAllSubstrings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&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="nx"&gt;anagramsCount&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;countAnagrams&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="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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;anagramsCount&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Maybe you have noticed, here I am checking first for duplicates, in order to know if I should continue further. As if there are no duplicated letters, then it's not possible to have an anagram.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And finally, we get all substrings into an array, iterate over it, count the anagrammatic pairs that are found and return this number.&lt;br&gt;
You can find the full code &lt;a href="https://github.com/mihailgaberov/misc/blob/master/coding-challenges/sherlock-and-anagrams.js"&gt;here&lt;/a&gt;.&lt;/p&gt;

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

&lt;p&gt;This kind of exercises are very good for making you think algorithmically, but also they change your way of working in your day to day job. My recommendation would be to do the same I am trying to do - train your brain now and then with one of those. And if you can - share. I know sometimes you don't have time for such challenges, but when you do - go for it.&lt;/p&gt;

&lt;p&gt;My personal feeling after finishing this was a total satisfaction, which is completely understandable, considering the the time it took me to do it. But at the end, dear reader, I am even happier I can share this experience with you 😌!&lt;/p&gt;

&lt;p&gt;🔥 Thanks for reading! 🔥&lt;/p&gt;


</description>
      <category>javascript</category>
      <category>codingchallenge</category>
      <category>sherlock</category>
    </item>
    <item>
      <title>How to optimize your JavaScript apps using Loops</title>
      <dc:creator>Mihail Gaberov</dc:creator>
      <pubDate>Tue, 23 Apr 2019 05:22:01 +0000</pubDate>
      <link>https://dev.to/mihailgaberov/optimizing-javascript-apps-loops-2kj2</link>
      <guid>https://dev.to/mihailgaberov/optimizing-javascript-apps-loops-2kj2</guid>
      <description>&lt;p&gt;Originally published at &lt;a href="https://mihail-gaberov.eu"&gt;mihail-gaberov.eu&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L3qdteQT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/mihailgaberov/mihail-gaberov.eu/raw/master/src/pages/optimizing-javascript-apps-loops/optimizing-javascript-apps-loops.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L3qdteQT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/mihailgaberov/mihail-gaberov.eu/raw/master/src/pages/optimizing-javascript-apps-loops/optimizing-javascript-apps-loops.jpg" alt=" How to optimize your JavaScript apps using Loops"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One of the easiest and most ignored thing to do, in order to boost up the performance of our JavaScript applications, is to learn how to write properly high performant loop statements. The idea behind this article is to help with that.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We will see the main types of loop used in JavaScript and how can we write them in a performant way.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's begin!&lt;/p&gt;

&lt;h2&gt;
  
  
  LOOP PERFORMANCE
&lt;/h2&gt;

&lt;p&gt;When it comes to loop performance, the debate is always about which loop to use. Which is the fastest and most performant? The truth is, that, of the four loop types provided by JavaScript, only one of them is significantly slower than the others - for-in loop. &lt;em&gt;The choice of loop type should be based on your requirements rather than performance concerns.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There are two main factors that contribute to loop performance - work done per iteration and number of iterations.&lt;br&gt;
In the sections below we will see how by decreasing them, we can have a positive overall impact to the loop performance.&lt;/p&gt;
&lt;h2&gt;
  
  
  For Loop
&lt;/h2&gt;

&lt;p&gt;ECMA-262, 3rd Edition, the specification that defines JavaScript's basic syntax and behavior, defines four types of loops. The first is the standard for loop, which shares its syntax with other C-like languages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&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="mi"&gt;10&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="c1"&gt;//loop body&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 probably the most commonly used JavaScript looping construct. In order to understand how can we optimize its work, we need to dissect it a little bit.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dissection
&lt;/h3&gt;

&lt;p&gt;The for loop consists of four parts: initialization, pretest condition, loop body, post-execute. The way it works is the following - first the initialization code is executed (var i = 0;), then the pretest condition (i &amp;lt; 10;). If the pretest condition evaluates to true, then the body of the loop is executed and after that the post-execute code (i++) is run.&lt;/p&gt;

&lt;h3&gt;
  
  
  Optimizations
&lt;/h3&gt;

&lt;p&gt;The first step in optimizing the amount of work in a loop is to minimize the number of object member and array item lookups.&lt;/p&gt;

&lt;p&gt;You can also increase the performance of loops by reversing their order. In JavaScript, reversing a loop does result in a small performance improvement for loops, provided that you eliminate extra operations as a result.&lt;/p&gt;

&lt;p&gt;Both of the statements above are valid for the other two faster loops as well (&lt;code&gt;while&lt;/code&gt; and &lt;code&gt;do-while&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// original loop&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;var&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;items&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="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&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="c1"&gt;// minimizing property lookups&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;var&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;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;items&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;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;len&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="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&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="c1"&gt;// minimizing property lookups and reversing&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;var&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;items&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="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  While Loop
&lt;/h2&gt;

&lt;p&gt;The second type of loop is the while loop. This is simple pretest loop, consisted of pretest condition and a loop body.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&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="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="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;//loop body&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Dissection
&lt;/h3&gt;

&lt;p&gt;If the pretest condition evaluates to true, the loop body is executed. If not - it's skipped. Every &lt;code&gt;while&lt;/code&gt; loop can be replaced with for and vice versa.&lt;/p&gt;

&lt;h3&gt;
  
  
  Optimizations
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// original loop&lt;/span&gt;
&lt;span class="kd"&gt;var&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;0&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;items&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;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&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="c1"&gt;// minimizing property lookups&lt;/span&gt;
&lt;span class="kd"&gt;var&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;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;items&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="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;count&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;items&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="c1"&gt;// minimizing property lookups and reversing&lt;/span&gt;
&lt;span class="kd"&gt;var&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;items&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="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;--&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;items&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Do-While Loop
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;do-while&lt;/code&gt; is the third type of loop and it's the only post-test loop in JavaScript. It is comprised of body loop and post-test condition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&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="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//loop body&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;++&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="na"&gt;10&lt;/span&gt;&lt;span class="err"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Dissection
&lt;/h3&gt;

&lt;p&gt;In this type of loop, the loop body is executed always at least once, then the post-test condition is being evaluated, and if it's true, another loop cycle is executed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Optimizations
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// original loop&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;k&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="k"&gt;do&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;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&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;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;items&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="c1"&gt;// minimizing property lookups&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;k&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;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;items&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="k"&gt;do&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;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&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;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// minimizing property lookups and reversing&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;items&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;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;do&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;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&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;k&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  For-In Loop
&lt;/h2&gt;

&lt;p&gt;The fourth and the last type of loop is called &lt;code&gt;for-in&lt;/code&gt; loop. It has very special purpose - &lt;strong&gt;enumerates the named properties of any JavaScript object&lt;/strong&gt;. Here it is how it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;prop&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;//loop body&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Dissection
&lt;/h2&gt;

&lt;p&gt;It's similar to the regular &lt;code&gt;for&lt;/code&gt; loop only by its name. The way it works is totally different. And this difference makes it much slower than the other three loops, which have equivalent performance characteristics such that it's not useful to try to determine which is fastest. Each time the loop is executed, the variable prop has the name of another property, which is a string, on the object. It will execute until all properties have been returned. These would be the properties of the object itself, as well as the ones inherited through its prototype chain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Notes
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;You should never use "for-in" to iterate over members of an array.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Each iteration through this loop causes a property lookup either on the instance or on the prototype, which makes the for-in loop much slower than the other loops. For the same number of iterations, it could be seven times slower than the rest.&lt;/p&gt;

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

&lt;p&gt;👍 The &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;while&lt;/code&gt; and &lt;code&gt;do-while&lt;/code&gt; loops all have similar performance characteristics, and so no one loop type is significantly faster or slower than the others.&lt;/p&gt;

&lt;p&gt;👍 Avoid the &lt;code&gt;for-in&lt;/code&gt; loop unless you need to iterate over a number of unknown object properties.&lt;/p&gt;

&lt;p&gt;👍 The best ways to improve loop performance are to decrease the amount of work done per iteration and decrease the number of loop iterations.&lt;/p&gt;

&lt;p&gt;I hope this was useful for you, as it was for me!&lt;/p&gt;

&lt;p&gt;🔥 Thanks for reading! 🔥&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.amazon.com/High-Performance-JavaScript-Application-Interfaces/dp/059680279X"&gt;"High Performance JavaScript" - Nicholas C. Zakas&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>optimizations</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
