Electron Long-running Tasks and Performance Troubleshooting
🎯 Introduction
Electron relies on application processes to handle windows and system operations. Processing large amounts of data continuously in the main process can temporarily make windows unresponsive. This article explains how to identify the problem, then introduces the roles of workers and utilityProcess.
🧱 Avoid blocking the main process
The following synchronous loop keeps the main process busy:
function doWork() {
for (let index = 0; index < 1000000000; index += 1) {
// Computational work
}
}
Until the loop finishes, the main process cannot handle window events promptly. Prefer the asynchronous APIs in node:fs/promises for file operations, and move intensive computation to a background task.
🛠 Choose a background task approach
| Task | Suitable approach |
|---|---|
| Computation in the page | Web Worker |
| JavaScript computation in the main process | utilityProcess or a Node.js worker |
| Running a separate command-line tool | child_process |
| Reading large files | Asynchronous file APIs or streams |
You do not need to learn every option at once. Start by asking whether a task will keep the current process busy for a long time.
🧹 Clean up windows and listeners
Timers, event listeners, and file handles can remain active after a window closes:
const timer = setInterval(() => {
console.log('Checking task status');
}, 5000);
win.on('closed', () => {
clearInterval(timer);
});
Whenever you create a resource, consider when to release it. Pay particular attention when creating windows repeatedly: avoid registering the same global listener each time.
🔍 Diagnose the symptoms
Work through these checks in order:
- Is only one button unresponsive, or is the entire window frozen?
- Is the main process running a synchronous loop or synchronous file operations?
- Have listeners or timers been registered more than once?
- Are large objects still referenced after the window closes?
- Do the Performance and Memory panels in DevTools show anything unusual?
Identify the type of problem before choosing a tool, rather than immediately switching threading strategies.
🧾 Summary
- The main process is not suitable for long-running synchronous computation.
- Asynchronous file APIs can reduce freezes caused by waiting for I/O.
- Workers, utilityProcess, and child processes suit different tasks.
- Clean up timers, listeners, and resources when a window closes.
- Start performance troubleshooting by observing symptoms, then identify the process involved.
❓ Questions and answers
Q1: Does using async/await guarantee that the app will not freeze?
No. await only waits for asynchronous operations. Long-running synchronous computation still blocks the current process.
Q2: Why clean up timers after a window closes?
Timers can keep running and retaining object references, causing unnecessary work and memory use.
🧪 Practice
Identify the part of the code below that could block the main process, and replace it with asynchronous file reading:
// Write your code here
🎉 You now know the basic approach to troubleshooting Electron performance.
Custom Protocols, Resource Loading, and Deep Links
Understand how resource loading differs between development and packaged apps, and when to use links that open specific content in your application.
PDF Printing and Report Export
Prepare a dedicated report page and use printToPDF to generate a PDF file that users can save.
