What’s lazy loading
Omar Ismail
Senior Software Engineer @ Digitinary | Java & Spring Expert ? | AWS & Microservices Architect ? | FinTech & Open Banking Innovator ?? | Digital Payments Expert ?? | Top 200 IT Content Creator in Jordan ?? | 40K+ ??
Who wants to wait for an application to be loaded? I don’t. Sometimes we choose to abandon the app than waiting. Getting the user to visit your website is not easy. So, losing him after all the effort is really a waste. Besides that, the first impressions matter. It’s our chance to win that visitor.
Therefore, we want to minimize the time between the user’s request for the app and the display of its first template. Lazy loading is our best friend for this problem.
Illustrative examples and files are based on the Angular app structure.
What’s lazy loading
Usually, when we think about lazy loading, we think of something slow. But our routes can be lazy and fast at the same time.
To avoid making the user waiting for the app to get started. We can split our application into small bandles. Therefore, download only the required files at the beginning and the other files when the user asks for them, which means download only on demand and, that’s called in Angular?lazy loading. Therefore, we will have something like this:
This time the main bundles will take less time since we don’t load everything. Thus, less to download and less for Angular to compile.?So, less time to start up the app. That improves startup time significantly, especially with enterprise applications.
Lazy loading is used to speed up our application’s startup time to give our app the best impression.
To get deeper with Angular, let’s examine how Angular build with and without lazy loading:
领英推荐
Let’s start the app in the -prod mode.
The first thing you’ll notice is that it takes much more time to compile. Let’s analyze the bundles:
If we want to reduce an app’s loading time, we should reduce the main bundle’s size.
And that’s where?lazy loading comes to the scene.
Its role is to build the main bundle that is essential for the app to start and display the initial page. Other bundles are loaded when needed. That’s why by the way, it’s called lazy loading. We only load on demand.
Takeouts
Thanks TO :FAM