Binge On Code

Jun 8, 2023

498 readers

Code Splitting And Lazy Loading

Code Splitting and lazy loading are basically augmentations of one another. Lazy loading builds on top of the power of code splitting.

Long story short, it is not a matter of which is better, it is a matter of how to use code splitting and lazy loading to get the best of both.

We will first understand what code splitting is all about, and its core benefit, then we will do the same for lazy loading. Then after that, get an understanding of how they work together to help you achieve maximum performance in your JavaScript web applications.

What is Code Splitting

Code splitting is the process of splitting up a large file or module into smaller manageable chunks of code, which can be loaded on demand.

This makes it very easy to separate concerns in your JavaScript module for example, and with modules comes the benefit of easy manageability.

You may have seen these in your React project or Angular projects after you compile them, there are those chunks files. Yes, these are analogous to what defines code splitting.

What is Lazy Loading

This is the process of loading only what is needed when it is needed. That is basically it.

You can lazy load script files when you need them and now, some browsers support even lazy loading images. But the focus here is lazy loading code, and thus scripts.

This is important since you only get to initially load that which the current screen of your application needs.

In the past, web applications had to load all the script files, well, things have changed for the better – you only get what you ordered for.

How They Work Together

Well, if you were to accomplish these two, you would have super fast loading times on your web application.

Even if they can be used in isolation, using code splitting for small sizes and lazy loading for on-demand availability of scripts will make your web application feel more perfomant.

So, use code splitting to reduce the size of your application module bundles and lazy loading to load the smaller chunks only when needed

Related Articles

Destructure JavaScript Object With Defaults

You can really simplify your access to your JavaScript objects by using this simple method to destructure JavaScript object with defaults.

Jun 20, 2023

Views 27

Get Index Of Item In JavaScript For Of Loop

In a few lines of code, you will see how easy it is to get an item’s index in JavaScript For Of Loop. Let’s do this!

Jun 16, 2023

Views 34

How To Provide Optional Property In JavaScript Object.

Let’s see how we can have JavaScript object optional property in a few lines of code. This can be really helpful, especially when you want to be concise as possible.

Jun 14, 2023

Views 9

Angular JavaScript React CSS Django Python React Native Next JS State Management Django Rest Framework Unity