<?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[Angular Magic - Medium]]></title>
        <description><![CDATA[Angular Magic represents a small community of Angular Developers which create open source projects for other developers. - Medium]]></description>
        <link>https://medium.com/angular-magic?source=rss----e7b8e8b6690a---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Angular Magic - Medium</title>
            <link>https://medium.com/angular-magic?source=rss----e7b8e8b6690a---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 12 Nov 2026 16:16:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/angular-magic" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Angular’s trackBy —  easier than ever]]></title>
            <link>https://medium.com/angular-magic/angulars-trackby-easier-than-ever-89e973389487?source=rss----e7b8e8b6690a---4</link>
            <guid isPermaLink="false">https://medium.com/p/89e973389487</guid>
            <category><![CDATA[angular-performance]]></category>
            <category><![CDATA[ngfor]]></category>
            <category><![CDATA[trackby]]></category>
            <category><![CDATA[angular-best-practices]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Cornilov Dan]]></dc:creator>
            <pubDate>Tue, 27 Jun 2023 10:08:51 GMT</pubDate>
            <atom:updated>2023-06-27T10:08:51.171Z</atom:updated>
            <content:encoded><![CDATA[<h3>Angular’s trackBy — easier than ever</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aNPw7LzC3pAE_0JPwqbr4A.jpeg" /></figure><p>Using <strong>trackBy</strong> is a typical solution to improve the performance of your Angular application.</p><p>This article does not cover how trackBy works, as there is already sufficient information available on this topic.</p><p>How does ChatGBT explain this?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S-qhaPRnJythhcC2MlK7AA.png" /></figure><h4>Problem</h4><p>Typically, when using <strong>trackBy</strong> in Angular, it is necessary to create a function that generates a unique value for each item. This method should be implemented in every component where trackBy is utilized.</p><p>Let’s suppose we have a couple of components that need to render a list of items.</p><pre>@Component({<br>  selector: &#39;app-cats&#39;,<br>  template: `<br>    &lt;ul&gt; <br>      &lt;li *ngFor=&quot;let item of items; trackBy: trackById&quot;&gt;<br>    &lt;/ul&gt;<br>  `,<br>  styleUrls: [&#39;./cats.component.scss&#39;],<br>})<br>export class CatsComponent {<br>  @Input() items: {id: number; name: string}[];<br><br> trackById(index, item): number {<br>     return item.id; <br>  }<br>}</pre><pre>@Component({<br>  selector: &#39;app-dogs&#39;,<br>  template: `<br>    &lt;ul&gt; <br>      &lt;li *ngFor=&quot;let item of items; trackBy: trackByIndex&quot;&gt;<br>    &lt;/ul&gt;<br>  `,<br>  styleUrls: [&#39;./dogs.component.scss&#39;],<br>})<br>export class DogsComponent {<br>  @Input() items: string[];<br><br> trackByIndex(index, item): number {<br>     return item.index; <br>  }<br>}</pre><p>As we can observe, there is a significant amount of boilerplate code that can be minimized.</p><h4>Solution</h4><p>To avoid code duplication and enhance the usability of trackBy, we employ a helpful technique known as PerformanceUtils.</p><pre>export class PerformanceUtils {<br>  static trackById(_index: number, item: any): string {<br>    return item.id;<br>  }<br><br>  static trackByIndex(index: number): number {<br>    return index;<br>  }<br>}</pre><p>Subsequently, the PerformanceUtils can be declared as a class property and utilized directly within the template.</p><pre>@Component({<br>  selector: &#39;app-cats&#39;,<br>  template: `<br>    &lt;ul&gt; <br>      &lt;li *ngFor=&quot;let item of items; trackBy: PerformanceUtils.trackById&quot;&gt;<br>    &lt;/ul&gt;<br>  `,<br>  styleUrls: [&#39;./cats.component.scss&#39;],<br>})<br>export class CatsComponent {<br>  @Input() items: {id: number; name: string}[];<br><br>  PerformanceUtils: typeof PerformanceUtils = PerformanceUtils;<br>}</pre><pre>@Component({<br>  selector: &#39;app-dogs&#39;,<br>  template: `<br>    &lt;ul&gt; <br>      &lt;li *ngFor=&quot;let item of items; trackBy: PerformanceUtils.trackByIndex&quot;&gt;<br>    &lt;/ul&gt;<br>  `,<br>  styleUrls: [&#39;./dogs.component.scss&#39;],<br>})<br>export class DogsComponent {<br>  @Input() items: string[];<br><br>  PerformanceUtils: typeof PerformanceUtils = PerformanceUtils;<br>}</pre><p>As we can see we have a lot of boilerplate code, which can be reduced, and now is very easy to integrate trackBy in every *ngFor of your application has become effortless by simply declaring one additional property.</p><p><strong>Connect with us on social media:<br></strong>🙉 Visit us on our website: <a href="https://angularmagic.com/">AngularMagic.com</a><br>🙈 Follow us on LinkedIn: <a href="https://www.linkedin.com/company/angular-magic/">LinkedIn</a><br>🙊 Follow us on Telegram: <a href="https://t.me/+e02uqe-wcsFhZjYy">Join link</a></p><p>🤌 Stay updated with our latest news, articles, and updates by following us on these platforms. We appreciate your support!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89e973389487" width="1" height="1" alt=""><hr><p><a href="https://medium.com/angular-magic/angulars-trackby-easier-than-ever-89e973389487">Angular’s trackBy —  easier than ever</a> was originally published in <a href="https://medium.com/angular-magic">Angular Magic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>