Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Tree shaking and dead code removal in Angular
📖 Scenario: You are building a simple Angular app that shows a list of fruits. Some helper functions are not used and should be removed automatically when building the app.
🎯 Goal: Learn how to set up an Angular component with data and configuration, use the data in the template, and understand how unused code can be removed by Angular's build process (tree shaking).
📋 What You'll Learn
Create an Angular standalone component with a list of fruits
Add a configuration variable to filter fruits by minimum length
Use a @for directive to display filtered fruits
Include an unused helper function to demonstrate dead code removal
💡 Why This Matters
🌍 Real World
In real Angular apps, unused code can make the app larger and slower. Tree shaking helps keep the app small and fast by removing code that is never used.
💼 Career
Understanding tree shaking is important for Angular developers to optimize app performance and reduce bundle size, which is a key skill in frontend development jobs.
Progress0 / 4 steps
1
Create the fruits list in the component
Create a standalone Angular component called FruitListComponent. Inside the component, create a fruits array with these exact string values: 'Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'.
Angular
Hint
Define a property called fruits inside the component class and assign the array with the exact fruit names.
2
Add a minimum length filter variable
Inside the FruitListComponent class, add a number variable called minLength and set it to 6. This will be used to filter fruits by their name length.
Angular
Hint
Add a number property minLength and assign it the value 6.
3
Filter fruits and display in template
In the component's template, use an Angular @for control flow to loop over fruits and @if to filter by length greater than or equal to minLength. Display each fruit inside a <li> element. Update the template string inside the @Component decorator accordingly.
Angular
Hint
Use @for (fruit of fruits; track fruit) to loop over fruits and @if (fruit.length >= minLength) to filter inside the template string.
4
Add an unused helper function to demonstrate dead code removal
Inside the FruitListComponent class, add a private method called unusedHelper that returns the string 'This function is not used'. This function will not be called anywhere, so Angular's build process should remove it automatically.
Angular
Hint
Add a private method unusedHelper that returns the exact string 'This function is not used'. Do not call it anywhere.
Practice
(1/5)
1. What is the main purpose of tree shaking in Angular?
easy
A. To remove unused code and imports during the build process
B. To add extra debugging information to the app
C. To increase the size of the final bundle
D. To automatically update Angular dependencies
Solution
Step 1: Understand tree shaking concept
Tree shaking is a process that removes unused code and imports from the final build.
Step 2: Identify its effect in Angular
In Angular, tree shaking helps reduce app size by excluding code that is never used.
Final Answer:
To remove unused code and imports during the build process -> Option A
Quick Check:
Tree shaking = remove unused code [OK]
Hint: Tree shaking removes unused code to shrink app size [OK]
Common Mistakes:
Thinking tree shaking adds code instead of removing
Confusing tree shaking with debugging tools
Believing tree shaking updates dependencies automatically
2. Which syntax correctly imports a module that can be tree shaken in Angular?
easy
A. import * as Component from '@angular/core';
B. import Component from '@angular/core';
C. require('@angular/core').Component;
D. import { Component } from '@angular/core';
Solution
Step 1: Recognize ES module import syntax
Angular uses ES module syntax: import { NamedExport } from 'module';
Step 2: Identify correct import for Component
The correct syntax is importing Component as a named export inside curly braces.
Final Answer:
import { Component } from '@angular/core'; -> Option D
Quick Check:
Correct ES module import = import { Component } from '@angular/core'; [OK]
Hint: Use curly braces for named imports in Angular [OK]
Common Mistakes:
Using default import syntax for named exports
Using CommonJS require instead of ES import
Importing everything as a namespace unnecessarily
3. Given this Angular service code, what will be removed by tree shaking if unusedMethod() is never called anywhere?