<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tooba Ali on Medium]]></title>
        <description><![CDATA[Stories by Tooba Ali on Medium]]></description>
        <link>https://medium.com/@toobaali1?source=rss-1045ed70972e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*HQvBvmMuvEe7UAPP.jpg</url>
            <title>Stories by Tooba Ali on Medium</title>
            <link>https://medium.com/@toobaali1?source=rss-1045ed70972e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 12:54:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@toobaali1/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Writing Optimized Code in JS By Understanding Hidden Classes]]></title>
            <link>https://medium.com/swlh/writing-optimized-code-in-js-by-understanding-hidden-classes-3dd42862ad1d?source=rss-1045ed70972e------2</link>
            <guid isPermaLink="false">https://medium.com/p/3dd42862ad1d</guid>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[hidden-class]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[optimization]]></category>
            <dc:creator><![CDATA[Tooba Ali]]></dc:creator>
            <pubDate>Tue, 17 Nov 2020 15:28:10 GMT</pubDate>
            <atom:updated>2021-02-19T18:39:41.868Z</atom:updated>
            <content:encoded><![CDATA[<p>JavaScript, as we know, is a dynamic programming language. What does that mean? Well, in a nutshell, it means that we can add, remove, or modify properties and methods of JS objects at run-time!</p><p>With that being a pro to JavaScript’s dynamic nature, there is a con to it too. JS objects are implemented as what is called a HASH TABLE in memory. Hash tables make it much slower to retrieve a property on an object as compared to the contiguous buffer method used in non-dynamic programming languages.</p><p>SO WHAT DO WE DO!? Well, the V8 JavaScript engine has got you covered. All you need to do as a developer is to learn about the<strong> solution</strong> provided by the V8 engine and to adopt coding practices accordingly for writing <em>better-optimized code.</em></p><p>The V8 engine provides the solution by providing a method called <strong>Hidden classes. </strong>The purpose of hidden classes is to optimize the access time when retrieving a property on an object. This is achieved by sharing hidden classes among objects created in a similar fashion.</p><p>Whenever an object is created in JavaScript, a hidden class is attached to it. Whenever a property is added to the object, “class transition” takes place i.e. the old hidden class switches to a new hidden class with the new property in it. Let’s understand this with an example.</p><pre><strong>function cupcake</strong>(frosting,sprinkles) {<br>	<strong>this</strong>.frosting <strong>=</strong> frosting;<br>	<strong>this</strong>.sprinkles <strong>=</strong> sprinkles;<br>}</pre><p>We have a constructor function cupcake that takes as argument the frosting type and the sprinkles type and whenever this function is invoked; we get an object that is our new Cupcake!</p><p>When V8 sees our cupcake constructor function is declared, it creates a hidden class say Class0. Now, when V8 sees on line 2, frosting being added as a property on cupcake, it updates the class0 with the new frosting property and switches from class0 to a new hidden class say class1. The same happens when sprinkles are added to the cupcake and class transition occurs from class1 to class2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*--iJn5491qcGy7mX-TKvAA.png" /><figcaption>Transition of hidden classes</figcaption></figure><p>Keeping this in mind, let’s create two cupcakes; chocCupcake and lemonCupcake:</p><pre>let chocCupake = new cupcake(chocolate, jimmies);<br>let lemonCupcake = new cupcake(lemon, sandingSugar);</pre><pre>//let&#39;s add a few more properties to our cupcakes so that they taste even better!</pre><pre>chocCupcake.syrup = &quot;chocolate&quot;;<br>choccCupcake.topping = &quot;cookie&quot;;</pre><pre>lemonCupcake.topping = &quot;cherry&quot;;<br>lemonCupcake.syrup = &quot;caramel&quot;;</pre><p>Now, adding new properties dynamically to our two cupcakes the way we did above; is a valid solution. <strong>BUT this is not an optimized solution! </strong>Why? Because the order in which we instantiated the two new properties<em> (namely syrup and topping)</em> is different! A different order results in the creation of two different hidden classes instead of sharing a common hidden class. This is what would happen under the hood:</p><p>V8 creates <strong>class0 </strong>for the constructor function cupcake. It transits to <strong>class1 </strong>for frosting and then to <strong>class2 </strong>for sprinkles. For chocCupcake, when it sees a property syrup being added it transits to <strong>class3a</strong><em>(class with syrup property) </em>and for topping to <strong>class4a</strong><em>(class with topping property)</em>. HOWEVER, for lemonCupcake V8 first sees the property topping being added to it and therefore instead of sharing the hidden class class3a, it creates a new hidden class <strong>class3b</strong><em>(class with topping property)</em>. It then creates a new hidden class for property syrup say <strong>class4b</strong><em>(class with syrup property)</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*1So-E4UbcKXFMBSOVr4RfA.png" /></figure><p>Since the two objects do not share the hidden classes (when they could have shared them); the access to the properties of the object is slow <em>because V8 is now not able to make use of </em><a href="https://github.com/sq/JSIL/wiki/Optimizing-dynamic-JavaScript-with-inline-caches"><em>inline caching</em></a>. If the two had shared the hidden class, V8 would have used inline caching for faster access to object properties!</p><p>Now that we know how V8 does all the working under the hood, it is important to keep in mind the <strong>TWO OPTIMIZATION TECHNIQUES </strong>when writing code in JavaScript:</p><ol><li>Try to assign all properties of an object in its constructor.</li><li>If still<em>(for some reason)</em>, you are dynamically adding new properties to the objects, always instantiate them in the <strong>SAME ORDER</strong> so that hidden classes may be shared among them!</li></ol><p>Happy Coding! 😄</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3dd42862ad1d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/writing-optimized-code-in-js-by-understanding-hidden-classes-3dd42862ad1d">Writing Optimized Code in JS By Understanding Hidden Classes</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Higher Order Components with React.js]]></title>
            <link>https://medium.com/swlh/understanding-higher-order-components-with-react-js-37ea2e7e140d?source=rss-1045ed70972e------2</link>
            <guid isPermaLink="false">https://medium.com/p/37ea2e7e140d</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[higher-order-components]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Tooba Ali]]></dc:creator>
            <pubDate>Mon, 26 Oct 2020 03:47:19 GMT</pubDate>
            <atom:updated>2020-10-26T03:47:19.350Z</atom:updated>
            <content:encoded><![CDATA[<p>New to the concept of HOC? Or having trouble understanding what HOC is and when to use it? Well, don’t worry! We will explore the concept of HOC with a real-life analogy and also with a coding example later in this post.</p><p>Have you ever seen a popcorn popper? You put in a corn kernel; a lot happens to it behind the scenes; and what comes out is a POPCORN!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FYmbd9FFkGz1lYurgeZ%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FYmbd9FFkGz1lYurgeZ%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FYmbd9FFkGz1lYurgeZ%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="324" frameborder="0" scrolling="no"><a href="https://medium.com/media/c86021c3485dc837636a0839331d71ae/href">https://medium.com/media/c86021c3485dc837636a0839331d71ae/href</a></iframe><p>A Higher Order Component (HOC) is nothing but a POPCORN POPPER! A corn kernel goes in and a POPCORN comes out!</p><pre>const withPopper = (cornKernel) =&gt; { <br>         <br><em>// all the heating and other complex things happen with the kernel here...</em></pre><pre>return popcorn;<br>}</pre><p>Whenever you crave for popcorn, you just have to put in corn kernels to the popper instead of having to reinvent the popper machine all over again! That’s how you will do it:</p><pre>let myPopcorn = withPopper(cornKernel);</pre><p>In case of HOC, all you need to remember is that a <strong><em>kernel</em></strong><em> </em>is a <strong>“React Component”</strong>, the <strong><em>popcorn </em></strong>is a<strong> “New, enhanced version of the React Component”</strong> whereas the <strong><em>popper </em></strong>is the <strong>HOC </strong>itself.</p><p>Formally, we define a Higher Order Component (HOC) as a function that takes in a component and returns a new and enhanced component.</p><p>Having understood the basic concept of a Higher Order Component with the popcorn analogy, let’s have a deeper look at how to use a HOC in React.js by diving into some React code.</p><p>A clothing brand wants its customers to be able to check out different color combinations of shirts and pants through an interactive user interface. A single click should change the color of shirt whereas a double-click is required to change the color of pants! This is what the final output should look like:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F9tMYkmAEzN5qUPFoMD%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F9tMYkmAEzN5qUPFoMD%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F9tMYkmAEzN5qUPFoMD%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="268" frameborder="0" scrolling="no"><a href="https://medium.com/media/79edeb2fa04c77d33738cdd2a7c56f2b/href">https://medium.com/media/79edeb2fa04c77d33738cdd2a7c56f2b/href</a></iframe><p>Let’s get started! 💪</p><p>First, we will make a Shirt component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c585aa96524c67cd64d83960720fc54e/href">https://medium.com/media/c585aa96524c67cd64d83960720fc54e/href</a></iframe><p>Now, we will make a Pants component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b455ff4c84c6e993761097acad02cdd4/href">https://medium.com/media/b455ff4c84c6e993761097acad02cdd4/href</a></iframe><p>We realize that we are repeating the same logic and same color changing code in both components’ local state. Imagine, sometime in future, the clothing brand introduces a new item <em>(let’s say hats)</em> and wants its color to be changed on a particular event <em>(say when a user hovers the mouse over the item)</em>. It would require us to REPEAT the same code all over again!</p><p>As a developer, our NUMBER ONE rule is <strong>D.R.Y (Don’t Repeat Yourself!)</strong>. And so, we need to find a solution to this repeating code problem! Well, this is when a higher order component comes in handy. Let’s create a HOC with the color changing logic:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e493f714cc14e1d33e1f00511503a118/href">https://medium.com/media/e493f714cc14e1d33e1f00511503a118/href</a></iframe><p>We see that we’ve pulled out our repeating code into a higher order component. The HOC takes in a component (WrappedComponent), adds our color-changing functionality to it and returns the new, enhanced version of the component(WithColorChange). Here’s how we can now re-write our other components:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2d30d4f39010799282af3d0235e8738/href">https://medium.com/media/c2d30d4f39010799282af3d0235e8738/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b846bb34c09a130ef8d1442aa77d0a00/href">https://medium.com/media/b846bb34c09a130ef8d1442aa77d0a00/href</a></iframe><p>Now that we have pulled all our logic out of the Shirt and Pants components into the withColorChange HOC, Shirt and Pants components are much simpler now. All we need to do is to wrap these inside our HOC. <em>(Note that we can also pass other information to our HOC like we’ve passed an array of items with both of our components).</em></p><p>So, whenever you find components with some repeating functionality, pull out that functionality into a HOC. Whenever, any of your component needs it; simply wrap it in the HOC and TA-DAAA! Your component now has the functionality added to it with minimal effort!</p><p>Here’s the <a href="https://github.com/toobaali1/hoc-clothing-brand">GitHub Link for the above example project</a>.</p><p>Happy Coding! 😄</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37ea2e7e140d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/understanding-higher-order-components-with-react-js-37ea2e7e140d">Understanding Higher Order Components with React.js</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>