{"id":1684,"date":"2023-10-25T05:00:00","date_gmt":"2023-10-25T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1684"},"modified":"2023-10-26T16:32:52","modified_gmt":"2023-10-26T23:32:52","slug":"perf-and-template-syntax-example-1","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/perf-and-template-syntax-example-1\/","title":{"rendered":"Perf and template syntax &#8211; Example 1"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Yesterday, I sent you <a href=\"http:\/\/performance-and-template-syntax\" target=\"_blank\" rel=\"noopener\" title=\"\">3 different examples of Angular binding syntaxes<\/a> and asked you to take a look at them and identify the pros and cons of each one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Today, let&#8217;s cover the pros and cons of our first example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example #1 \u2013 Array of data<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>&lt;div *ngFor=\"let data of getData()\"&gt;<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The only pro of this example is the simplicity of the syntax. Other than that, the syntax uses one of the anti-patterns covered earlier in this newsletter: <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/anti-pattern-series-calling-a-method-in-a-template\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Calling a method in a template<\/a>. The problem is that this method will be called a lot more than you&#8217;d think, and if that code happened to create a new array every single time, it could be really bad in terms of performance, as Angular would have to re-render the entire list pretty much every time the user does anything in the application.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>What can we do to improve it?<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Use a variable instead of a method &#8211; this fixes the performance issue right away:<br><br><code>&lt;div *ngFor=\"let data of dataList\"&gt;<\/code><br><\/li>\n\n\n\n<li>Use a <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/ngfor-trackby-for-improved-performance\/\" target=\"_blank\" rel=\"noopener\" title=\"\">trackBy function<\/a> to avoid useless re-renders. By telling Angular what&#8217;s the unique ID of each list item, Angular would be less confused, but it&#8217;s a sub-par solution:<br><br><code>&lt;div *ngFor=\"let data of getData(); trackBy: trackById\"&gt;<\/code><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to iterate over data that&#8217;s in your component (not an observable or a Signal), then use a variable instead of calling a method. Easy enough.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, I sent you 3 different examples of Angular binding syntaxes and asked you to take a look at them and identify the pros and cons of each one. Today, let&#8217;s cover the pros and cons of our first example: Example #1 \u2013 Array of data &lt;div *ngFor=&#8221;let data of getData()&#8221;&gt; The only pro of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,21,7],"tags":[],"class_list":["post-1684","post","type-post","status-publish","format-standard","hentry","category-angular","category-architecture","category-template-syntax"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1684","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1684"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1684\/revisions"}],"predecessor-version":[{"id":1692,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1684\/revisions\/1692"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}