Angular Material Impact on Download Size
I was interested in using Angular Material component library because I thought it would help me build web apps that look better than my usual efforts. My introductory experience with it was very promising and I think it'll deliver on design aesthetics. The project was also an experiment to see other parts of Angular in action. On the topic of download size, the result was not as good.
One aspect of using Angular that sounded appealing was that it came with an already-configured build pipeline so I didn't have to build my own. Starting from tools like TypeScript compiler, through to tools like Webpack. It is a multi-stage process to go from source code to a compact package delivered by our web server. It performs optimizations like "tree-shaking" for removing unused code, and "minification" which removed comments, whitespaces, everything nonessential. Ideally this meant the final package would have only what we absolutely need and no more. In practice, I'm not so sure.
After finishing the "Tour of Heroes" tutorial and playing with a bit of CSS, but before I converted over to using Angular Material, my application's raw download size was a little over 309 kilobytes.
✔ Browser application bundle generation complete.
✔ Copying assets complete.
✔ Index html generation complete.
Initial Chunk Files | Names | Raw Size | Estimated Transfer Size
main.e962da6362f99387.js | main | 274.74 kB | 71.19 kB
polyfills.716cc9a230a87077.js | polyfills | 33.11 kB | 10.70 kB
runtime.6e50dac17b84e715.js | runtime | 922 bytes | 517 bytes
styles.19b697cb6d7ac4c1.css | styles | 477 bytes | 172 bytes
| Initial Total | 309.22 kB | 82.56 kB
After I installed Angular Material with "ng add @angular/material
" but before I actually explicitly imported any components, the final bundle size grew by over 50% to 479 kilobytes:
✔ Browser application bundle generation complete.
✔ Copying assets complete.
✔ Index html generation complete.
Initial Chunk Files | Names | Raw Size | Estimated Transfer Size
main.7c4c0522a82c6f83.js | main | 338.94 kB | 87.07 kB
styles.4aae9080591fb05a.css | styles | 106.13 kB | 9.64 kB
polyfills.716cc9a230a87077.js | polyfills | 33.11 kB | 10.70 kB
runtime.6e50dac17b84e715.js | runtime | 922 bytes | 517 bytes
| Initial Total | 479.08 kB | 107.92 kB
This was disappointing. As far as I know I haven't actually used anything of Angular Material, yet I had already picked up 170 kilobytes of stuff that the automated optimization tools could not avoid. And even worse, this was completely opaque to me. I don't know of any way to break down where those 170kB came from, nor what I could try to optimize them.
As I started actually importing and using components from Angular Material, my bundle size grew as expected. I had hoped maybe those 170 kilobytes were majority of core shared code and each additional module would add only minimal size, but this was definitely not the case. My freshman effort at Angular Material conversion added the following modules.
import { MatAutocompleteModule } from '@angular/material/autocomplete';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatListModule } from '@angular/material/list';
import { MatToolbarModule } from '@angular/material/toolbar';
And by doing so, I picked up almost 300 additional kilobytes for a total of 776 kilobytes.
✔ Browser application bundle generation complete.
✔ Copying assets complete.
✔ Index html generation complete.
Initial Chunk Files | Names | Raw Size | Estimated Transfer Size
main.dc2316f7fe149792.js | main | 636.43 kB | 134.91 kB
styles.7a21ac1018e81662.css | styles | 105.66 kB | 9.52 kB
polyfills.716cc9a230a87077.js | polyfills | 33.11 kB | 10.70 kB
runtime.6e50dac17b84e715.js | runtime | 922 bytes | 517 bytes
| Initial Total | 776.11 kB | 155.63 kB
Warning: bundle initial exceeded maximum budget. Budget 500.00 kB was not met by 276.11 kB with a total of 776.11 kB.
Final line of this output is a warning from Angular's build-time bundle size budget watcher, which is half of a good feature. It's great that it informed me I exceeded size budget, but I don't understand what exactly contributed to that total and I certainly don't know what I could do about it. Well, I guess there's "Don't use Angular Material" but I hope for something finer-grained than that.
Does this matter? It depends on the context. If I'm writing an app for my own use on my home network, a few hundred kilobytes are insignificant. If I'm writing something to run on my phone via mobile data, such data usage would be notable but not necessarily a deal breaker. If I'm creating a browser app from ESP32, there's only 4MB of flash memory to serve such files and a few hundred kilobytes could sink the entire project.
It's just like any other tool: there are right places to use it, and there are wrong places to use it. This applies to Angular itself, to Angular Material component library, and whatever I use for layout. Because for both good and ill, layout is not an intrinsic part of Angular framework.