Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 36 ยท Testing and Performance

Startup Performance and Responsive Windows

Make an Electron app start fast and stay responsive: measure startup, defer work, lazy-load modules, show the window at the right moment, keep the main process free and use utility processes.

Electron apps have a reputation for being slow to start and heavy to run. Part of it is earned: every app carries Chromium and Node. But most of the slowness users actually feel comes from what the app does at startup and what it does on the main thread, and both are in your control.

This lesson gives you a method: measure first, move work out of the critical path, load code on demand, and keep long jobs away from the thread that draws and responds. The runnable examples measure and compare strategies with real timers, so the numbers are yours.

Measure before you change anything

Startup has several phases: the process launches, app.whenReady() resolves, the window is created, the page loads, and the first useful content appears. Put marks in each:

JavaScript
// main.js (first lines)
const t0 = performance.now();
const mark = (label) => console.log(`${label}: ${(performance.now() - t0).toFixed(0)} ms`);

app.whenReady().then(() => {
  mark('ready');
  const win = new BrowserWindow({ show: false });
  win.once('ready-to-show', () => { mark('ready-to-show'); win.show(); });
  win.webContents.once('did-finish-load', () => mark('did-finish-load'));
  win.loadFile('index.html');
});

In the renderer, performance.mark and performance.measure plus the Performance tab of DevTools show the page's own part (see Memory and Debugging). Optimise the largest slice first; guessing wastes time.

A tiny profiler

The same idea works as a helper that times named steps and reports the slowest. This one runs a pretend startup sequence and prints a breakdown.

Node.js Timing the phases of a startup

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Two of those steps (update check and indexing) are not needed to show the first window. They are the obvious candidates to move out of the critical path.

Defer what is not needed to draw the window

Order startup by what the user needs first:

  1. Create the window and start loading the UI.
  2. Everything else (update checks, telemetry, indexing, restoring secondary windows) runs after ready-to-show or did-finish-load, ideally when the event loop is idle.
JavaScript
win.once('ready-to-show', () => {
  win.show();
  setTimeout(() => {          // a beat later, off the critical path
    checkForUpdates();
    buildSearchIndex();
  }, 3000);
});

Use show: false plus ready-to-show to avoid the white flash of an unpainted window, and set a backgroundColor that matches your theme so even slow loads look intentional.

Load code only when needed

Every require or import costs parse and execution time, in both processes. Lazy-load features that are rarely used:

JavaScript
ipcMain.handle('export:pdf', async (_e, doc) => {
  const { exportPdf } = await import('./export-pdf.js');   // loaded on first use
  return exportPdf(doc);
});

Common heavy hitters: PDF and image libraries, large date or markdown libraries, native modules, anything that reads the disk at import time. In the renderer, split the bundle with dynamic import() so the first screen ships only what it needs, and use a bundler (Vite, esbuild) so hundreds of tiny files become a few. Fewer files also means a faster ASAR read.

The lazy pattern in miniature: wrap an expensive module in a function that loads once.

Node.js Lazy loading with a cached import

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Keep the main process free

The main process is one thread. It creates windows, dispatches IPC and drives native menus. If it blocks for 200 ms, every window of your app freezes for 200 ms, including its title bar. The usual culprits:

  • fs.readFileSync, execSync, spawnSync and large JSON.parse calls in handlers.
  • CPU-heavy loops (hashing, image processing, searching).
  • Synchronous IPC (ipcRenderer.sendSync), which blocks the renderer and the main process.

Rules of thumb: use the async APIs, never sendSync, and move heavy work off the main thread. Electron gives you three places to do that:

Where Use it for
Worker thread (node:worker_threads) CPU work on data: parsing, compressing, hashing
Utility process (utilityProcess.fork) isolated services, crash containment, native modules
Hidden renderer or Web Worker work needing browser APIs such as canvas or WASM
JavaScript
import { utilityProcess } from 'electron';

const child = utilityProcess.fork(path.join(__dirname, 'indexer.js'));
child.postMessage({ type: 'index', folder });
child.on('message', (msg) => mainWindow.webContents.send('index:progress', msg));

See the freeze, then fix it

This example runs a heavy loop on the main thread alongside a "heartbeat" that should tick every 20 ms. The gap in the ticks is what a user perceives as a frozen window. Then the same work runs in a worker thread and the heartbeat stays steady.

Node.js Blocking the thread versus using a worker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The main-thread run shows a gap of hundreds of milliseconds, the worker run stays near the 20 ms you asked for. That difference is "the app froze" versus "the app stayed smooth".

Renderer performance

Everything you know about web performance applies: avoid layout thrashing, virtualise long lists, debounce input handlers, use CSS transforms for animation and requestAnimationFrame for visual updates. A few Electron-specific tips:

  • Disable backgroundThrottling only for windows that really must run timers while hidden; it costs battery.
  • Hide rather than destroy windows you will reopen soon (win.hide()), if memory allows.
  • Use contextBridge functions that return small results; sending a 50 MB object over IPC clones it and stalls both sides. Send file paths or stream chunks instead.
  • Use the V8 compile cache: require('v8-compile-cache') or Node's module.enableCompileCache() stores compiled code between launches.
  • Avoid big synchronous work in DOMContentLoaded; paint something first.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What should you do before optimising startup?

    Marks and a breakdown show where the time actually goes, so you fix the biggest slice instead of guessing.
  2. 2. Why create the window with show false and wait for ready-to-show?

    ready-to-show fires after the first render, so the window appears with content instead of flashing white.
  3. 3. Which of these blocks every window of the app?

    The main process is a single thread that serves all windows and menus. A long synchronous task freezes them all.
  4. 4. Why avoid ipcRenderer.sendSync?

    Synchronous IPC stalls the sender and ties it to main-process responsiveness. Use invoke and await the result.

Summary

  • Measure startup phases; optimise the largest first.
  • Create the window early with show: false, show on ready-to-show, and defer updates, indexing and telemetry.
  • Lazy-load heavy modules in both processes and bundle the renderer.
  • Never block the main process: avoid sync APIs and sendSync; use worker threads, utility processes or renderer workers.
  • Send small messages over IPC; pass paths or streams instead of huge objects.

Next: Memory, Debugging and Profiling