Bird
Raised Fist0
Angularframework~8 mins

Lifecycle execution order mental model in Angular - Performance & Optimization

Choose your learning style10 modes available
Performance: Lifecycle execution order mental model
MEDIUM IMPACT
This concept affects how quickly Angular components initialize and update, impacting the time to interactive and smooth user experience.
Running heavy logic in lifecycle hooks
Angular
ngOnInit() {
  this.scheduleHeavyTask(); // defer heavy work
}
scheduleHeavyTask() {
  if ('requestIdleCallback' in window) {
    requestIdleCallback(() => {
      // heavy computation deferred
    });
  } else {
    setTimeout(() => {
      // heavy computation deferred
    }, 0);
  }
}
Defers heavy work to idle time, allowing initial rendering and interaction to happen faster.
📈 Performance GainReduces blocking time during lifecycle hooks, improving INP by 50-100ms
Running heavy logic in lifecycle hooks
Angular
ngOnInit() {
  for(let i = 0; i < 1000000; i++) {
    // heavy computation
  }
}
ngAfterViewInit() {
  // more heavy DOM manipulation
}
Heavy logic blocks the main thread during component initialization and view rendering, delaying user interaction readiness.
📉 Performance CostBlocks rendering for 100+ ms, causing slow INP and delayed interactivity
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Heavy logic in ngOnInitMinimal DOM0Low[X] Bad
Heavy logic deferred outside lifecycle hooksMinimal DOM0Low[OK] Good
Multiple DOM updates in ngOnChanges and ngAfterViewCheckedMultiple DOM updates2+High[X] Bad
Batched DOM updates with requestAnimationFrameSingle DOM update1Medium[OK] Good
Rendering Pipeline
Angular lifecycle hooks run during component initialization and updates, affecting style calculation, layout, and paint stages depending on DOM changes triggered.
→Style Calculation
→Layout
→Paint
⚠️ BottleneckLayout (reflows caused by DOM updates in lifecycle hooks)
Core Web Vital Affected
INP
This concept affects how quickly Angular components initialize and update, impacting the time to interactive and smooth user experience.
Optimization Tips
1Avoid heavy synchronous work inside lifecycle hooks to prevent blocking rendering.
2Batch DOM updates inside lifecycle hooks to minimize reflows and repaints.
3Defer non-critical computations using scheduling APIs to improve input responsiveness.
Performance Quiz - 3 Questions
Test your performance knowledge
Which lifecycle hook is best suited for deferring heavy computations to avoid blocking initial rendering?
AngOnInit with deferred scheduling
BngAfterViewChecked with immediate execution
CngOnChanges with synchronous DOM updates
DngAfterContentInit with blocking loops
DevTools: Performance
How to check: Record a performance profile while interacting with the component. Look for long tasks during lifecycle hook execution and multiple layout events.
What to look for: Long scripting times blocking main thread and multiple layout/repaint events indicate poor lifecycle hook performance.

Practice

(1/5)
1. Which Angular lifecycle hook runs first when a component receives new input values?
ngOnInit, ngOnChanges, ngAfterViewInit, or ngOnDestroy?
easy
A. ngOnChanges
B. ngOnInit
C. ngAfterViewInit
D. ngOnDestroy

Solution

  1. Step 1: Understand lifecycle hooks on input change

    When input properties change, Angular calls ngOnChanges first to react to those changes.
  2. Step 2: Identify the first hook called on input update

    ngOnInit runs only once after the first ngOnChanges, so it is not first.
  3. Final Answer:

    ngOnChanges -> Option A
  4. Quick Check:

    Input changes trigger ngOnChanges first [OK]
Hint: Remember: ngOnChanges always runs before ngOnInit on input change [OK]
Common Mistakes:
  • Thinking ngOnInit runs before ngOnChanges
  • Confusing ngAfterViewInit as first hook
  • Assuming ngOnDestroy runs on input change
2. Which of the following is the correct syntax to implement the ngOnDestroy lifecycle hook in an Angular component?
easy
A. destroy() { console.log('Destroyed'); }
B. onDestroy() { console.log('Destroyed'); }
C. ngOnDestroy() { console.log('Destroyed'); }
D. ngDestroy() { console.log('Destroyed'); }

Solution

  1. Step 1: Recall Angular lifecycle hook naming

    Angular lifecycle hooks have specific method names starting with 'ng', so ngOnDestroy is correct.
  2. Step 2: Match method name exactly

    Only ngOnDestroy() { console.log('Destroyed'); } uses the exact method name ngOnDestroy required by Angular.
  3. Final Answer:

    ngOnDestroy() { console.log('Destroyed'); } -> Option C
  4. Quick Check:

    Lifecycle hooks start with 'ngOn' [OK]
Hint: Lifecycle hooks always start with 'ngOn' followed by event [OK]
Common Mistakes:
  • Using incorrect method names like onDestroy or destroy
  • Missing 'ngOn' prefix
  • Confusing lifecycle hook names with custom methods
3. Given this Angular component code snippet:
export class MyComponent implements OnInit, OnChanges {
  @Input() data: string;

  ngOnChanges() { console.log('Changes detected'); }
  ngOnInit() { console.log('Init called'); }
}

What will be logged first when the component receives its first input value?
medium
A. Init called
B. Changes detected
C. No logs
D. Both logs at the same time

Solution

  1. Step 1: Understand lifecycle order on first input

    When the component first receives input, Angular calls ngOnChanges before ngOnInit.
  2. Step 2: Identify which log appears first

    The ngOnChanges method logs 'Changes detected' first, then ngOnInit logs 'Init called'.
  3. Final Answer:

    Changes detected -> Option B
  4. Quick Check:

    ngOnChanges runs before ngOnInit [OK]
Hint: ngOnChanges logs before ngOnInit on first input [OK]
Common Mistakes:
  • Assuming ngOnInit runs before ngOnChanges
  • Thinking logs happen simultaneously
  • Ignoring ngOnChanges on first input
4. You notice your Angular component's ngOnDestroy method is not called when navigating away. What is the most likely cause?
medium
A. The component is never destroyed because it is cached or reused
B. You forgot to implement OnDestroy interface
C. You used ngOnDestroy instead of onDestroy
D. Angular does not call ngOnDestroy on navigation

Solution

  1. Step 1: Understand when ngOnDestroy runs

    ngOnDestroy runs when Angular destroys the component instance.
  2. Step 2: Identify why it might not run on navigation

    If the component is cached or reused (e.g., via route reuse strategy), it is not destroyed, so ngOnDestroy won't run.
  3. Final Answer:

    The component is never destroyed because it is cached or reused -> Option A
  4. Quick Check:

    ngOnDestroy runs only on actual destruction [OK]
Hint: ngOnDestroy runs only if component is truly destroyed [OK]
Common Mistakes:
  • Confusing interface implementation with method call
  • Using wrong method name
  • Believing Angular never calls ngOnDestroy on navigation
5. Consider an Angular component with these lifecycle hooks:
ngOnChanges() { console.log('Changes'); }
ngOnInit() { console.log('Init'); }
ngAfterViewInit() { console.log('After View'); }
ngOnDestroy() { console.log('Destroy'); }

If the component receives new input twice, then is removed from the page, what is the correct order of console logs?
hard
A. "Changes", "Init", "Changes", "After View", "Destroy"
B. "Init", "Changes", "After View", "Changes", "Destroy"
C. "Changes", "Changes", "Init", "After View", "Destroy"
D. "Changes", "Init", "After View", "Changes", "Destroy"

Solution

  1. Step 1: Order on first input and init

    First input triggers ngOnChanges, then ngOnInit runs once, then ngAfterViewInit after view setup.
  2. Step 2: Order on second input and destroy

    Second input triggers ngOnChanges again. Finally, when component is removed, ngOnDestroy runs.
  3. Final Answer:

    "Changes", "Init", "After View", "Changes", "Destroy" -> Option D
  4. Quick Check:

    ngOnChanges before ngOnInit; ngAfterViewInit after init; ngOnDestroy last [OK]
Hint: Remember: ngOnChanges runs on every input change, ngOnInit once [OK]
Common Mistakes:
  • Placing ngOnInit before first ngOnChanges
  • Mixing order of ngAfterViewInit and ngOnChanges
  • Forgetting ngOnDestroy runs last
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard