{"id":590,"date":"2023-03-01T05:00:00","date_gmt":"2023-03-01T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=590"},"modified":"2024-10-02T17:20:27","modified_gmt":"2024-10-03T00:20:27","slug":"lazy-loading-for-better-angular-performance","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/lazy-loading-for-better-angular-performance\/","title":{"rendered":"Lazy-loading for better Angular performance"},"content":{"rendered":"\n<p>Before we continue our series on standalone components, it is important to talk about the most important tool at our disposal to create more performant Angular applications: <a href=\"https:\/\/angular.dev\/guide\/ngmodules\/lazy-loading\" target=\"_blank\" rel=\"noopener\" title=\"\">Lazy-loading<\/a>.<\/p>\n\n\n\n<p>When we use lazy-loading, instead of building our application as one single bundle of code that gets downloaded as soon as our user accesses the app in a browser, we divide our code into several different pieces that get downloaded <strong>on-demand when they are needed<\/strong>.<\/p>\n\n\n\n<p>For instance, without lazy-loading, if our application is 25 MB big in terms of Javascript code, a browser has to download, then parse, and run those 25 MB of code, which can slow things down a lot on mobile devices or slower internet connections.<\/p>\n\n\n\n<p>Instead, we can <strong>divide our application into different modules<\/strong> containing components, pipes, directives, and services. In our example, let&#8217;s assume we create an <code>AdminModule<\/code> that includes all of the features needed for the Admin section of the application. If this module ends up containing 10 MB of code and we use lazy-loading with it, then the initial bundle of the application is down from 25 MB to 15 MB, which is a big difference.<\/p>\n\n\n\n<p>Only Admin users would ever have to download the 10 MB of code for the admin section, which is great for both performance and safety (hackers can&#8217;t reverse-engineer code that has never been downloaded in their browser).<\/p>\n\n\n\n<p>The best part of lazy-loading is that a single line of code initiates it in our router config:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"227\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-65-1024x227.png\" alt=\"\" class=\"wp-image-591\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-65-1024x227.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-65-300x67.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-65-768x170.png 768w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-65.png 1388w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above line will get the Angular compiler to automatically create a bundle of code for <code>AdminModule<\/code> and enable lazy-loading of that code when <code>\/items<\/code> is accessed. That&#8217;s it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before we continue our series on standalone components, it is important to talk about the most important tool at our disposal to create more performant Angular applications: Lazy-loading. When we use lazy-loading, instead of building our application as one single bundle of code that gets downloaded as soon as our user accesses the app in [&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,20],"tags":[],"class_list":["post-590","post","type-post","status-publish","format-standard","hentry","category-angular","category-architecture","category-performance"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/590","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=590"}],"version-history":[{"count":5,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/590\/revisions"}],"predecessor-version":[{"id":2184,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/590\/revisions\/2184"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}