R
ROHIT SINGH
Guest
When we build Angular applications, performance is one of the top priorities. No user likes a frozen screen or unresponsive UI, especially in data-heavy applications. But when you perform CPU-intensive tasksβlike parsing a huge JSON, encrypting data, or running mathematical calculationsβthe main thread gets blocked.
This leads to:
Janky scrolling
Slow clicks
A poor overall experience
So how do we solve this?
Enter Web Workers.
In this blog, weβll explore:
What Web Workers are
Why theyβre important in Angular apps
Step-by-step implementation in Angular
Real-world examples
Limitations & best practices
What Are Web Workers?
Think of Web Workers as background assistants.
They run JavaScript code in a separate thread, leaving the main UI thread free to handle rendering, animations, and user interactions.
In simple terms:
The main thread is your Angular app UI.
A Web Worker is a helper that does heavy lifting in the background.
Example: Imagine youβre cooking and also talking with a guest. If you try to do everything yourself, youβll be slow and distracted. Instead, if you hire an assistant to chop vegetables (background work), you can focus on the conversation (UI).
Why Use Web Workers in Angular?
Here are some clear benefits of using workers in your Angular projects:
Benefit Impact on Angular App
Non-blocking UI Keeps the app smooth
Parallel Processing Utilizes multiple CPU cores
Better UX No frozen screens
Scalable Performance Handles large datasets without lag
Typical use cases in Angular:
Processing large arrays or JSON files
Data encryption/decryption
Image compression or resizing
Complex financial or statistical calculations
Setting Up Web Workers in Angular
Angular CLI makes adding Web Workers simple.
Step 1: Generate a Web Worker
Run:
This will:
Create src/app/app.worker.ts
Update angular.json with worker support
Step 2: Write Worker Logic
Inside app.worker.ts:
Step 3: Call Worker from Component
In app.component.ts:
Now, instead of freezing the UI for seconds, the calculation runs in the background.
Example: With vs Without Web Worker
Without Worker (UI freezes)
The user canβt click buttons or scroll until this finishes.
With Worker (UI stays responsive)
The UI remains smooth while the worker processes the task.
Real-World Angular Use Cases
Data-Heavy Dashboards β Large datasets (e.g., stock market apps) processed in workers while UI shows charts.
Image Uploads β Compress or resize images in workers before uploading, improving performance.
Cryptography Apps β Encrypt/decrypt data in workers for better security without blocking the UI.
Machine Learning Models β Run ML predictions in workers without slowing down UI animations.
Limitations of Web Workers
Before you add workers everywhere, note these drawbacks:
No direct access to the DOM (document, window)
Debugging is harder than normal code
Communication between worker and main thread adds overhead
Not ideal for tiny tasks (use only for CPU-heavy logic)
Best Practices
β Use workers for long-running tasks (more than 50ms)
β Limit the number of workers (too many = memory overhead)
β Consider RxJS + Web Workers for reactive data pipelines
β Always handle worker termination when not in use
Conclusion
Web Workers in Angular are a powerful way to keep your apps responsive.
By offloading heavy computations to a background thread, you:
Improve user experience
Ensure smooth UI interactions
Unlock the true potential of modern CPUs
Next time your Angular app feels sluggish with big data or calculations, donβt block the main threadβuse a Web Worker.
Try it in your project today and see the difference!
If you found this useful, share it with your Angular developer friends!
Read writing from
Rohit Singh
on Medium. Full-stack developer with 6+ years in Angular, Node.js & AWS. Sharing tips, best practices & real-world lessons from building scalable apps.
medium.com
Continue reading...
This leads to:



So how do we solve this?
Enter Web Workers.
In this blog, weβll explore:
What Web Workers are
Why theyβre important in Angular apps
Step-by-step implementation in Angular
Real-world examples
Limitations & best practices

Think of Web Workers as background assistants.
They run JavaScript code in a separate thread, leaving the main UI thread free to handle rendering, animations, and user interactions.
In simple terms:
The main thread is your Angular app UI.
A Web Worker is a helper that does heavy lifting in the background.


Here are some clear benefits of using workers in your Angular projects:
Benefit Impact on Angular App
Non-blocking UI Keeps the app smooth
Parallel Processing Utilizes multiple CPU cores
Better UX No frozen screens
Scalable Performance Handles large datasets without lag
Typical use cases in Angular:





Angular CLI makes adding Web Workers simple.

Run:
Code:
ng generate web-worker app
This will:
Create src/app/app.worker.ts
Update angular.json with worker support

Inside app.worker.ts:
Code:
/// <reference lib="webworker" />
addEventListener('message', ({ data }) => {
// Example: Heavy calculation
let result = 0;
for (let i = 0; i < data; i++) {
result += i;
}
postMessage(result); // Send back result
});

In app.component.ts:
Code:
export class AppComponent {
result!: number;
calculateHeavyTask() {
if (typeof Worker !== 'undefined') {
const worker = new Worker(new URL('./app.worker', import.meta.url));
worker.onmessage = ({ data }) => {
this.result = data;
console.log('Result from worker:', data);
};
worker.postMessage(1000000000); // Send data to worker
} else {
console.log('Web Workers not supported.');
}
}
}
Now, instead of freezing the UI for seconds, the calculation runs in the background.


Code:
let result = 0;
for (let i = 0; i < 1000000000; i++) {
result += i;
}
console.log(result);
The user canβt click buttons or scroll until this finishes.

Code:
worker.postMessage(1000000000);
The UI remains smooth while the worker processes the task.

Data-Heavy Dashboards β Large datasets (e.g., stock market apps) processed in workers while UI shows charts.
Image Uploads β Compress or resize images in workers before uploading, improving performance.
Cryptography Apps β Encrypt/decrypt data in workers for better security without blocking the UI.
Machine Learning Models β Run ML predictions in workers without slowing down UI animations.

Before you add workers everywhere, note these drawbacks:





β Use workers for long-running tasks (more than 50ms)
β Limit the number of workers (too many = memory overhead)
β Consider RxJS + Web Workers for reactive data pipelines
β Always handle worker termination when not in use

Web Workers in Angular are a powerful way to keep your apps responsive.
By offloading heavy computations to a background thread, you:
Improve user experience
Ensure smooth UI interactions
Unlock the true potential of modern CPUs
Next time your Angular app feels sluggish with big data or calculations, donβt block the main threadβuse a Web Worker.



Rohit Singh
β Medium
Read writing from


Continue reading...