Electron Long-running Tasks and Performance Troubleshooting

Identify main process blocking, choose an appropriate approach to background tasks, and learn the basics of resource cleanup.

🎯 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

TaskSuitable approach
Computation in the pageWeb Worker
JavaScript computation in the main processutilityProcess or a Node.js worker
Running a separate command-line toolchild_process
Reading large filesAsynchronous 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:

  1. Is only one button unresponsive, or is the entire window frozen?
  2. Is the main process running a synchronous loop or synchronous file operations?
  3. Have listeners or timers been registered more than once?
  4. Are large objects still referenced after the window closes?
  5. 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.