Dev Discussions: How to Reduce your Angular Bundle Size with Google Developers Expert Santosh Yadav
Tags: angular, javascript, beginners, webdev
Published: 2022-07-18 11:24:40

If you ever have questions about Angular, [Santosh Yadav](https://twitter.com/SantoshYadavDev?ref_src=twsrc^google|twcamp^serp|twgr^author) is the person to ask.

Not only is he an [Auth0 ambassador](https://auth0.com/ambassador-program) and a [GitHub Star](https://stars.github.com/), but he’s currently certified as a [Google Developers Expert](https://developers.google.com/community/experts) for Angular. 

Santosh jumped into the deep end with Angular early in his career—building apps, teaching others, and attending conferences in his spare time. That dedication to Angular worked out, as he’s now a leading [MEAN stack](https://en.wikipedia.org/wiki/MEAN_(solution_stack)) trainer and developer, with his own popular[ Angular-focused YouTube channel](https://www.youtube.com/c/TechTalksWithSantosh/videos) to boot.

[Santosh recently sat down with CoderPad Developer Advocate Corbin Crutchley](https://www.twitch.tv/videos/1314167351) to discuss one Angular topic that’s particularly popular these days – reducing the bundle size of angular apps.

Bundle sizes impact the performance of a website. If you have a bundle of greater than 1MB, then your user has to wait longer for the browser to:

1. Download that bundle
2. Parse the bundle
3. Display contents of the bundle

While this might not seem like a big deal, each MB can take an extra second, which can easily cause a user to lose interest and navigate away from the site – not exactly a good business practice.

Santosh loves using Angular to reduce bundle size and points out four ways Angular can help you do this:

1. Tree shaking via Angular Ivy
2. Lazy loading
3. Source map analyzers
4. Standalone components

## Bundle reduction tip #1: Use tree shaking

A typical library will ship with hundreds of functions, but rarely will your application need to use all of them.

This presents a problem in that importing the *entire* library can drastically increase your bundle size.

*Tree shaking* is a way to only import the functions of a module your application will use. 

![A comparison of component map with tree shaking. On the left is one index.js file with 3 modules. One module has three files, and one of those files has three functions. In total this beforee images has four files and 11 functions that will be build. On the right is the after tree shaking image where one file has one module, that module has one file, and that file has one function that will be included in the bundle.](https://d2h1bfu6zrdxog.cloudfront.net/wp-content/uploads/2022/07/img_62d1791786900.png)[via Aaditya Sharma on StackOverflow](https://stackoverflow.com/questions/45884414/what-is-tree-shaking-and-why-would-i-need-it/55936233#55936233)

Depending on the size of the library you’re utilizing, you can drastically cut down on your bundle size. As a bonus, you don’t need to do anything to do tree shaking in your Angular application – it’s been a part of Angular Ivy since Angular 8.

If you’re not familiar with it, Ivy is – at its simplest – an ultra-efficient rendering pipeline.

![An image with four boxes labelled "rendering pipelien in angular". The first box says "template html", then an arrow to the next box which says "template data", then an arrow to the next box which says "angular interpreter", and finally an arrow pointing to a box that says "DOM".](https://d2h1bfu6zrdxog.cloudfront.net/wp-content/uploads/2022/07/img_62d17918bda46.png)The steps Angular Ivy takes to render updates to the DOM. [Via AngularMinds](https://www.angularminds.com/blog/article/what-is-angular-ivy.html)

In addition to reducing the bundle size, you’ll also get [the following benefits](https://blog.angular.io/version-9-of-angular-now-available-project-ivy-has-arrived-23c97b63cfa3) with Ivy:

- Faster testing
- Better debugging
- Improved CSS class and style binding
- Improved type checking
- Improved build errors
- Improved build times, enabling AOT on by default
- Improved Internationalization

## Bundle reduction tip #2: Lazy loading

While tree shaking will prevent completely unused pieces of code from making it into your bundle, you’ll often have functions that are not used when a user first logs into your application but will be used later.

Lazy loading creates multiple chunks of code that are only loaded when that particular code is used in the application.

This can really reduce the size of the application, especially if you split libraries into different feature modules that are only used when needed by the application.

![A component map labelled Lazy loading along with the Angular symbol. The map has several components, some are marked "lazy" and the rest are not. ](https://d2h1bfu6zrdxog.cloudfront.net/wp-content/uploads/2022/07/img_62d179191890b.png)Example component structure with lazy loading. Via [9lessons.info](https://i.ytimg.com/vi/vLIIhzLGmxA/maxresdefault.jpg)

While lazy loading isn’t just limited to Angular (you can use it in React via [React.lazy](https://reactjs.org/docs/code-splitting.html) as well), when paired with the other bundle-reducing technology it makes Angular quite an attractive framework for creating smaller applications.

## Bundle reduction tip #3: Source map analyzers

A source map analyzer is precisely what it sounds like – it analyzes the different files, components, and modules of your application to show you how big they are.

You can then use this information to see where you can improve your code base’s efficiency to reduce its size. Santosh found source map analyzers particularly useful to reduce the size of a component with a large bundle size due to a CSS issue.

![A source analysis map that shows the size of different files and folders in relation to one another.](https://d2h1bfu6zrdxog.cloudfront.net/wp-content/uploads/2022/07/img_62d17919984d0.png)Example results of a source map analysis. Via [DigitalOcean](https://www.digitalocean.com/community/tutorials/angular-bundle-size).

As demonstrated in the example above, you can see which dependencies take up the most space. If they’re configured wrong, you’ll be able to tell immediately.

Alternatively, it may help you decide to migrate away to different dependencies to reduce the bundle size further.

## Bundle reduction tip #4: Standalone components

One of the newest Angular features to help you reduce bundle size are standalone components:

“Standalone components, directives, and pipes aim to streamline the authoring experience by reducing the need for NgModules,” according to the [Angular developer documentation](https://angular.io/guide/standalone-components).

Angular is unique in frontend frameworks because you must register your components. 

Standalone components force you to have more explicit imports, which enables you to side-step the need for tree-shaking in some places because you’re just not manually including what you don’t need.

## How to avoid Angular overdose

As much as Santosh loves working with Angular, he’s also a big advocate of work-life balance. He has advice for would-be workaholics and [how managers can help prevent employee burnout](https://dev.to/this-is-learning/how-i-made-workplace-toxic-1ici) – and you can watch him talk about those in the [Twitch playback video here](https://www.twitch.tv/videos/1314167351).

------

Interested in more conversations with developers about their tech stacks? Check out these other *Dev Discussions*:

- [Creating Fantastic User Interfaces with Esther Agbaje of Chakra UI](https://coderpad.io/blog/development/user-interfaces-with-esther-agbaje-chakra/)
- [Intro to the Jamstack with Salma Alam-Naylor of Netlify](https://coderpad.io/blog/development/intro-to-jamstack-with-salma-alam-naylor/)
- [SQL vs. NoSQL with James Quick of PlanetScale](https://coderpad.io/blog/development/sql-vs-nosql-with-james-quick/)