<?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: A developer</title>
    <description>The latest articles on DEV Community by A developer (@mansi_shrivastava_0c1b7a7).</description>
    <link>https://dev.to/mansi_shrivastava_0c1b7a7</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%2F2564670%2Fa4615bca-9c64-4f63-9c11-dcecca6fd920.jpeg</url>
      <title>DEV Community: A developer</title>
      <link>https://dev.to/mansi_shrivastava_0c1b7a7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mansi_shrivastava_0c1b7a7"/>
    <language>en</language>
    <item>
      <title>How I fixed a performance issue in a dropdown that rendered 4000 options .</title>
      <dc:creator>A developer</dc:creator>
      <pubDate>Sat, 14 Dec 2024 03:35:54 +0000</pubDate>
      <link>https://dev.to/mansi_shrivastava_0c1b7a7/how-i-fixed-a-performance-issue-in-a-dropdown-that-rendered-4000-options--13p</link>
      <guid>https://dev.to/mansi_shrivastava_0c1b7a7/how-i-fixed-a-performance-issue-in-a-dropdown-that-rendered-4000-options--13p</guid>
      <description>&lt;p&gt;So, I have this goal to render lots of options in my Angular application. Obviously, the data is coming from the API, and I was using a simple forEach loop to display that. But the issue is performance. Is it ideal to use forEach for rendering options? I guess not, right? So, I figured out some options for you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Virtual scrolling&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;cdk-virtual-scroll-viewport appendOnly itemSize="50" class="example-viewport"&amp;gt;
  &amp;lt;div *cdkVirtualFor="let item of items" class="example-item"&amp;gt;{{item}}&amp;lt;/div&amp;gt;
&amp;lt;/cdk-virtual-scroll-viewport
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reference — &lt;a href="https://material.angular.io/cdk/scrolling/overview" rel="noopener noreferrer"&gt;https://material.angular.io/cdk/scrolling/overview&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setup&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install npm install @angular/cdk&lt;/li&gt;
&lt;li&gt;Add CDK to Your Project ng add @angular/cdk&lt;/li&gt;
&lt;li&gt;Import the following module
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ScrollingModule } from '@angular/cdk/scrolling';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use in your HTML&lt;br&gt;
&lt;a href="https://media2.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%2Fqknm8a5w868mvorbqia0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fqknm8a5w868mvorbqia0.png" alt="Image description" width="800" height="802"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;cdk-virtual-scroll-viewport itemSize="70" class="viewport"&amp;gt;
  &amp;lt;div *cdkVirtualFor="let option of options" class="item"&amp;gt;
    {{ option }}
  &amp;lt;/div&amp;gt;
&amp;lt;/cdk-virtual-scroll-viewport&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Lazy Load Scroll&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div *ngFor="let option of options" (scroll)="onScrollDropdown()"&amp;gt;
  {{ option }}
&amp;lt;/div&amp;gt;

onScrollDropdown() {
  if (this.isNearBottom()) {
    this.loadMoreOptions();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The purpose of Lazy Load Scroll is to load more items as the user scrolls down the list, improving performance by fetching data only when it’s needed&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Track By&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div *ngFor="let user of users; trackBy:userByName"&amp;gt;
{{user.name}} -&amp;gt; {{user.score}}
&amp;lt;/div&amp;gt;

public function userByName(index: number, user: User): id {
  return user?.id;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The purpose of using trackBy is to assign a unique identity to each element in a list. If Angular finds two elements with the same identity, it only updates the content if it has changed. Without trackBy, Angular relies on object references, which often change even if the content stays the same. This can cause Angular to unnecessarily re-render the entire list.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading 👍&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Mansishrivastava" rel="noopener noreferrer"&gt;Visit my website&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As I continue to enhance this post with additional frameworks, I will make further edits to improve it. Hopefully, these updates will provide more clarity and value.&lt;br&gt;
&lt;a href="https://mansishrivastava12.medium.com/how-i-fixed-a-performance-issue-in-a-dropdown-that-rendered-4000-options-914ae86e9cbc" rel="noopener noreferrer"&gt;https://mansishrivastava12.medium.com/how-i-fixed-a-performance-issue-in-a-dropdown-that-rendered-4000-options-914ae86e9cbc&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>ui</category>
      <category>ux</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
