DEV Community

Abanoub Kerols
Abanoub Kerols

Posted on

Angular AOT vs JIT: The Complete Guide with Practical Example (v20)

In modern Angular (v20+), understanding the difference between AOT (Ahead-of-Time) and JIT (Just-in-Time) compilation can help developers significantly optimize performance, debugging, and build time. In this article, we’ll explain the difference, how they work, when to use each, and howenableProdMode() ties into this process.

🔍 What Is JIT (Just-in-Time)?

JIT is the default compilation mode when running Angular in development using:

ng serve

In this mode:

  • Angular compiles templates in the browser at runtime.
  • Ideal for development: you get faster builds and live reload.
  • Debugging tools and developer warnings (like change detection issues) are enabled.
  • File size is larger, and performance is slower than AOT.

🚀 What Is AOT (Ahead-of-Time)?

AOT compiles your Angular code at build time using:

ng build --configuration=production

In this mode:

  • Angular compiles templates into optimized JavaScript before shipping to the browser.
  • You get faster runtime performance.
  • Compiler is not shipped to the browser (more secure and smaller bundle).
  • enableProdMode()is triggered automatically.

🧠 Role of enableProdMode()

The method enableProdMode() is part of Angular core. When activated, it disables Angular's development-specific checks and tools (like debug data or console warnings).

In main.ts, it usually looks like this:

import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

Enter fullscreen mode Exit fullscreen mode

Angular CLI calls this automatically in production builds.

⚙️ Example Using Angular v20 (Standalone Component)

Here’s a simple example to show AOT +enableProdMode() in action:

main.ts

import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

bootstrapApplication(AppComponent)
  .catch(err => console.error(err));
Enter fullscreen mode Exit fullscreen mode

app.component.ts

import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <h1>Hello Angular v20 🚀</h1>
    <p>Counter: {{ counter() }}</p>
    <button (click)="increment()">Increment</button>
  `
})
export class AppComponent {
  counter = signal(0);

  constructor() {
    if (typeof ngDevMode !== 'undefined' && ngDevMode) {
      console.log('🔧 Development Mode');
    } else {
      console.log('✅ Production Mode');
    }
  }

  increment() {
    this.counter.update(v => v + 1);
  }
}

Enter fullscreen mode Exit fullscreen mode

🧪 Where Are AOT and JIT Files Located?

 Feature                 JIT                         AOT
______________       ______________             ______________
Compilation     | In browser (runtime)     During ng build (buildtime)
Output in /dist |  ❌ No permanent files    Compiled JS is in /dist
Compiler Shipped|        ✅Yes                     ❌ No
File size       |         Bigger                  Smaller
Startup Speed   |         Slower                  Faster
Enter fullscreen mode Exit fullscreen mode

Check AOT code in /dist/main.js:
You’ll see code like:

ɵɵelementStart(0, "h1");
ɵɵtext(1, "Hello");
ɵɵelementEnd();
Enter fullscreen mode Exit fullscreen mode

✅ Conclusion: Which Should You Use?

 use Case                Recommendation
______________           ______________             
Development      |      Use JIT (ng serve)     
Production       |      Use AOT (ng build --prod)  
Server-side apps |      Use AOT                      
Enter fullscreen mode Exit fullscreen mode

Want better performance? ✅ Use AOT + enableProdMode() in production.
Need fast debugging? 🛠️ Use JIT during development.

Top comments (0)