<?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 Ronen Mars on Medium]]></title>
        <description><![CDATA[Stories by Ronen Mars on Medium]]></description>
        <link>https://medium.com/@ronenmars?source=rss-95c29abc8c2b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*uv_jyZMhpgXnEauzn37b1w.jpeg</url>
            <title>Stories by Ronen Mars on Medium</title>
            <link>https://medium.com/@ronenmars?source=rss-95c29abc8c2b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 19:20:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ronenmars/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[How to Deal with Performance Issues in JS Deconstruction]]></title>
            <link>https://medium.com/bigpanda-engineering/how-to-deal-with-performance-issues-in-js-deconstruction-ae6d8bcbb534?source=rss-95c29abc8c2b------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae6d8bcbb534</guid>
            <category><![CDATA[js]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[deconstruction]]></category>
            <dc:creator><![CDATA[Ronen Mars]]></dc:creator>
            <pubDate>Tue, 31 Mar 2020 23:06:53 GMT</pubDate>
            <atom:updated>2020-04-02T12:46:06.129Z</atom:updated>
            <content:encoded><![CDATA[<p>This article explores exactly at which point immutability affects our code performance, how to analyze it and a few solution options.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p9c85xSpcAGbi7CR1N6GtQ.jpeg" /></figure><p>Lately, we noticed that one of our components is lagging a bit. The lag varied with each client in the system — one page loaded smoothly while another page’s calculation process took 8 seconds. Our UI was stuck and lagging and we were officially concerned.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h6Cdsnhx52B7W7nW9FOBbw.png" /><figcaption>The frontend is struggling sometimes</figcaption></figure><p>When we looked into it, we found that the UI loading delay started when we compressed 10K objects from one structure to another.</p><p>The purpose of the code snippet was to turn this:</p><pre>[<br> {<br>  name: &#39;host&#39;,<br>  demos: [<br>      {<br>        demoName: &#39;Presentation&#39;<br>      },<br>      {<br>        demoName: &#39;Meetup&#39;<br>      },<br>      {<br>        demoName: &#39;Conference&#39;<br>      }<br>    ]<br>  }, {<br>  name: &#39;criticality&#39;,<br>  demos: [<br>  {<br>    demoName: &#39;Urgent&#39;<br>  },{<br>    demoName: &#39;Low&#39;<br>  }]<br> }<br>];</pre><p>Into this:</p><pre>[<br>    {<br>      name: &#39;host&#39;,<br>      usedIn: [&#39;Presentation&#39;,&#39;Meetup&#39;,&#39;Conference&#39;] <br>    },<br>    {<br>      name: &#39;criticality&#39;,<br>      usedIn: [&#39;Urgent&#39;,&#39;Low&#39;]<br>    }<br>]</pre><h3><strong>Analysis</strong></h3><p>The first version of our processing code looked like this:</p><pre>const getDemoNamesFromArray = (rawObjArray) =&gt; {<br>  const transformedObjs = rawObjArray<br>    .reduce((displayObj, obj) =&gt; ({<br>      ...displayObj,<br>      ...{<br>        [obj.name]:<br>          [<br>            ...new Set([<br>              ...(displayObj[obj.name] || []),<br>              ...obj.demos.map((x) =&gt; x.demoName)<br>            ])<br>          ]<br>      }<br>    }), {});<br>const organizedObjs = Object.keys(transformedObjs)<br>    .map((objName) =&gt; ({<br>      name: objName,<br>      usedIn: transformedObjs[objName]<br>    }));<br>return organizedObjs;<br>}</pre><p><strong>Can you spot what’s wrong with this code?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qkJ0vcpNea8H1b9HnqWhVQ.png" /></figure><p>We pushed every demo as an object property, flatting the values into a string using a map function. We used a new Set() to keep it immutable and unique.</p><p>Processing 10K objects took us about 8 seconds…</p><p><strong>How did we get to 8 seconds</strong>? How can we analyze our code for such complex processes?</p><p><strong>First, we tried chunking.</strong></p><p>We took the same code and broke the list of demos down into an array of 25 demos each and processed much smaller datasets — 25 demos instead of 10K demos at once. In order to allow the UI ongoing rendering, we added a 500ms timeout to each promise, giving the UI a break to breathe between calculations.</p><pre>const processIncidentDemos = async (demos) =&gt; {<br>  const transformFunc = (chunkNames) =&gt; chunkNames.reduce((demosResult, demo) =&gt; ({<br>    ...demosResult,<br>    ...{<br>      [demo.name]:<br>          [<br>            ...new Set([<br>              ...(demosResult[demo.name] || []),<br>              ...demo.demos.map((x) =&gt; x.demoName)<br>            ])<br>          ]<br>    }<br>  }), {});</pre><pre>const transformedDemos = (demoObj) =&gt; Object.keys(demoObj)<br>    .map((demoName) =&gt; ({<br>      name: demoName,<br>      usedIn: demoObj[demoName]<br>    }));</pre><pre>const promiseArr = _.chunk(demos, 25).map((demosChunk) =&gt;<br>    new Promise((r) =&gt; setTimeout(r(transformFunc(demosChunk)), 500)));</pre><pre>return Promise.all(promiseArr).then((res) =&gt; res.reduce((acc, i) =&gt; Object.assign({}, acc, i), {})).then(transformedDemos);<br>};</pre><p>This helped somewhat but it was not enough. The main component (modal) loaded without major lag but we could still see that we had smaller resolution lags, we could see the typing cursor lagging in a few fields in inner components. My colleague <a href="https://medium.com/u/9f66ce4cb53d">Dor Sever</a> suggested we treat the problem by analyzing the root cause of it.</p><h3>The Solution</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*euMwWUpwikATSCATJH6-Zw.png" /></figure><p><a href="https://medium.com/u/827c327d5d0c">Dor</a> took the code processing and did the same calculation using lodash, but this time without using Sets and without building the object property by property.</p><p>First, he took the demos and grouped them by name into a new array with all of the demo as [name, demoName]. He transformed the demos into a new object, without checking the uniqueness of the demoName, which made an instant and extraordinary improvement to the performance: we reduced the calculation time from 8 seconds to <strong>0.06 seconds</strong>:</p><pre>const getNamesFromDemos = (demos) =&gt; {<br>  // Group all tags by name and flat the source systems<br>  const demosGroupByName = _.chain(demos).groupBy(&#39;name&#39;).map((value, key) =&gt;<br>    ([key, value.map((x) =&gt; x.demos.map((y) =&gt; y.demoName))])).value();</pre><pre>  // Transform the tags into an object<br>  const transformedDemos = {};<br>  demosGroupByName.forEach((data) =&gt; {<br>    transformedDemos[data[0]] =<br>      data[1][0];<br>  });</pre><pre>// Return transformed tags in needed structure<br>  return Object.keys(transformedDemos)<br>    .map((demoName) =&gt; ({<br>      name: demoName,<br>      usedIn: transformedDemos[demoName]<br>    }));<br>};</pre><p>I later decided to remove the lodash and re-organize it all in pure JS without additional libraries (like lodash). I’ve refactored the logic to this piece of code, which serves the same purpose as any of the snippets mentioned above but it does it natively. GroupBy of lodash was replaced by a reduce function, _.chain was deconstructed into a few nested functions.</p><pre>const getSuggestionsFromDemos = (demos) =&gt; {<br>  const groupBy = function (xs, key) {<br>    return xs.reduce((rv, x) =&gt; {<br>      (rv[x[key]] = rv[x[key]] || []).push(x);<br>      return rv;<br>    }, {});<br>  };</pre><pre>  // Demos grouped by name<br>  const groupedDemos = groupBy(demos, &#39;name&#39;);</pre><pre>  // Flat demos names and return organized array</pre><pre>// For every key in demosOrganized<br>  const demosOrganized = Object.keys(groupedDemos).map((singleDemo) =&gt; {<br>    // Take demo<br>    const demoNames = Object.values(groupedDemos[singleDemo]).map(<br>      // Flatten names<br>      (demo) =&gt; demo.demos.map((demoProps) =&gt; demoProps.demo))[0];</pre><pre>// Return tag with it source systems<br>    return (<br>      {<br>        name: singleDemo,<br>        usedIn: demoNames<br>      }<br>    );<br>  });<br>  // Return transformed demos in needed structure<br>  return demosOrganized;<br>};</pre><p>Now that we had some perspective, we saw that the thing that had stolen processing time from us was the new Set() creation. Every time we did it, we checked whether the demo name existed in demosResult in order to maintain the uniqueness of every field (which we solved using groupBy, but let’s shelf that for now).</p><h3>TL;DR</h3><p>We started a process that caused a small bug in one of our modals. To fix the bug, we had to try a few solutions and choose the best one. Of course, calculations of this type should be at the backend, but in order to keep existing architecture we had to do it at our frontend. After I understood the purpose of the code and figured out how to execute it best, I decided the optimal solution would be to use native code.</p><p>I hope you’ve enjoyed reading this article as much as I’ve enjoyed writing it. Feel free to share, and if you have any questions or thoughts about the concepts I used or another point of view about improving performance, I would love to hear from you on my twitter<a href="https://twitter.com/ronenmars"> @RonenMars</a>.</p><h3>Who Am I?</h3><p>Ronen Mars is an inspired full-stack web developer working at<a href="http://bigpanda.io/"> BigPanda.io</a>, specializing in Frontend and architecture. He is passionate about JS, new technologies, and enjoys funny parrot gifs and film photography.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae6d8bcbb534" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bigpanda-engineering/how-to-deal-with-performance-issues-in-js-deconstruction-ae6d8bcbb534">How to Deal with Performance Issues in JS Deconstruction</a> was originally published in <a href="https://medium.com/bigpanda-engineering">BigPanda Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>