<?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>Forem: Saura</title>
    <description>The latest articles on Forem by Saura (@montai).</description>
    <link>https://forem.com/montai</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%2F324783%2F8be37e49-8b71-4f68-89dc-6995c35514d2.jpeg</url>
      <title>Forem: Saura</title>
      <link>https://forem.com/montai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/montai"/>
    <language>en</language>
    <item>
      <title>Ideas on improving array element access and making web apps performant</title>
      <dc:creator>Saura</dc:creator>
      <pubDate>Thu, 11 Aug 2022 15:08:32 +0000</pubDate>
      <link>https://forem.com/montai/ideas-on-improving-array-element-access-and-making-web-apps-performant-554f</link>
      <guid>https://forem.com/montai/ideas-on-improving-array-element-access-and-making-web-apps-performant-554f</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Tiny things matter when it comes to the performance of a web application. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's take a very intuitive problem where we wonder how web developers will access the last element from an array of items that contains ~1 million items in it. &lt;/p&gt;

&lt;p&gt;The obvious thoughts are using the slice, pop array methods or via accessing the index or in many other ways below:-&lt;/p&gt;

&lt;p&gt;let data = [...] =&amp;gt; ~millions of items.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let last = data.slice(-1)[0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let last = data[data.length - 1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let last = [...data].pop()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let last = data.reduceRight(x =&amp;gt; x)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let last = data.at(-1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or by using external libraries like lodash and any jQuery methods.&lt;/p&gt;

&lt;p&gt;After running some tests on these sets of instructions for an array of millions of items we have got a performance benchmark!&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5im93of5rbjtxnw8osz5.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5im93of5rbjtxnw8osz5.png"&gt;&lt;/a&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsvriwbalcim3kv1drsb2.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsvriwbalcim3kv1drsb2.png"&gt;&lt;/a&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8b7l3uiez2tushtzdxa.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8b7l3uiez2tushtzdxa.png"&gt;&lt;/a&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp4t575zarwoycr4zqpmj.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp4t575zarwoycr4zqpmj.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Conclusions - It's recommended to use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let last = data[data.length - 1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which is the fastest cross-browser solution among all possible solutions.&lt;br&gt;
After that my personal choice will be to use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let last = data.at(-1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let last = [...data].pop()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although, [...data].pop() takes an extra space of O(N) and time to create a shallow copy of the big data array but it's faster.&lt;br&gt;
Solutions using jQuery are the slowest on all browsers.&lt;br&gt;
Thanks to StackOverflow and Kamil Kiełczewski. &lt;br&gt;
&lt;a href="https://stackoverflow.com/users/860099/kamil-kie%c5%82czewski" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>array</category>
      <category>optimisation</category>
    </item>
    <item>
      <title>Improve on choosing the better data structures</title>
      <dc:creator>Saura</dc:creator>
      <pubDate>Thu, 11 Aug 2022 06:24:33 +0000</pubDate>
      <link>https://forem.com/montai/improve-on-choosing-the-better-data-structures-imk</link>
      <guid>https://forem.com/montai/improve-on-choosing-the-better-data-structures-imk</guid>
      <description>&lt;p&gt;In many organisations including B2C e-commerce, the choice of data structures is not good when writing code that scales customers on the web browser.  &lt;strong&gt;DOM manipulations and client-side operations are costly!&lt;/strong&gt; Any big operations you write/perform/query directly on the web browser using javascript are costly. &lt;/p&gt;

&lt;p&gt;Say, for instance, the choice of setting up the data structure of a &lt;strong&gt;shopping cart containing many items&lt;/strong&gt; that requires frequent update/addition/removal.&lt;br&gt;
Instead of structuring a shopping cart data items like a list of items as shown below -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "headers": [
    "name",
    "quantity",
    "price"
  ],
  "items": [
    {
      "id": "1232ewr2",
      "name": "Inoculation",
      "quantity": "1",
      "price": "20",
      "sku": "432EGFE"
    },
    {
      "id": "e632r32",
      "name": "Pulp Fiction",
      "quantity": "3",
      "price": "5",
      "sku": "432ERFE"
    },
    ...
  ],
  "total": 35
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "headers": [
    "name",
    "quantity",
    "price"
  ],
  "items": {
    "1232ewr2": {
      "name": "Inoculation",
      "quantity": "1",
      "price": "20",
      "sku": "432EGFE"
    },
    "e632r32": {
      "name": "Pulp fiction",
      "quantity": "3",
      "price": "5",
      "sku": "432ERFE"
    },
    ...
  },
  "total": 35
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you observe critically and think, there are a few observations that determine the use-cases and improvement on time constraints on the web browser -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Searching&lt;/strong&gt; an item in the cart takes - &lt;strong&gt;O(1)&lt;/strong&gt; instead of &lt;strong&gt;O(N)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Updating&lt;/strong&gt; the quantity (addition/removal) of an item in the cart takes  &lt;strong&gt;O(1)&lt;/strong&gt; instead of &lt;strong&gt;O(N)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Removing&lt;/strong&gt; an item from the cart takes &lt;strong&gt;O(1)&lt;/strong&gt; instead of &lt;strong&gt;O(N)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For frequent addition/removal / updation of items - Associative containers like &lt;strong&gt;Maps&lt;/strong&gt; come in handier than linear data structures for instance &lt;strong&gt;Arrays / Linked lists&lt;/strong&gt;.&lt;br&gt;
Like handling large-scale geospatial data, repeated data containing metadata and storing a set of fixed keywords or tags that are referenced frequently.&lt;br&gt;
So, take your time to analyse your problem first and then determine the basic operations that must be supported and finally choose the right data structure to quantify the resource constraints for each operation.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>data</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
