DEV Community

Pocket PDF
Pocket PDF

Posted on AI-assisted

Web Workers for File Processing: What They Fix and What They Don't

A browser-based file tool can have a perfectly reasonable interface and still feel broken.

The user selects a file, clicks Process and the entire page stops responding.

The browser hasn't necessarily crashed. JavaScript might simply be doing too much work on the main thread.

This is where Web Workers become useful.

They're not a solution to every performance problem, but they can make a noticeable difference in applications that perform CPU-heavy file operations.

Why the main thread becomes a problem

JavaScript running on the main thread shares time with the browser's interface work.

That includes responding to clicks, updating the page, handling input and rendering visual changes.

Suppose an application needs to process a large document or encode an image.

If that operation runs synchronously for several seconds, the browser may not get enough opportunities to update the interface.

The user clicks the button again because nothing appears to happen.

Sometimes they refresh the page.

Neither outcome is particularly helpful.

Moving work into a Web Worker

A Web Worker runs JavaScript in a separate background thread.

A basic example looks like this:

// main.js
const worker = new Worker(
new URL("./processor.js", import.meta.url),
{ type: "module" }
);

worker.postMessage({
type: "PROCESS",
file
});

worker.onmessage = (event) => {
const { type, result, error } = event.data;

if (type === "SUCCESS") {
console.log("Processing complete", result);
}

if (type === "ERROR") {
console.error(error);
}
};

The worker can receive the file and perform the processing independently.

// processor.js
self.onmessage = async (event) => {
try {
const { file } = event.data;

const buffer = await file.arrayBuffer();

// Perform the actual processing here.

self.postMessage({
  type: "SUCCESS",
  result: buffer
});
Enter fullscreen mode Exit fullscreen mode

} catch (error) {
self.postMessage({
type: "ERROR",
error: error.message
});
}
};

This is a simplified example, not a complete production implementation. Real applications also need cancellation, progress reporting, cleanup and validation.

Workers have limitations

There are a few things worth understanding before moving everything into a worker.

First, workers cannot directly manipulate the DOM. If you need to update a progress bar, the main thread must receive messages and update the interface.

Second, transferring large amounts of data between threads can introduce overhead.

Structured cloning may create additional copies of data. Transferable objects such as ArrayBuffers can avoid some of that copying when ownership can be transferred safely.

Third, not every browser library works inside a worker. Some depend on browser APIs that are only available in a window context.

There's also memory consumption to consider. Moving processing into a worker doesn't mean the operation suddenly requires less RAM.

It mainly changes where the work happens.

Don't move everything into a worker automatically

A tiny text transformation probably doesn't need a separate worker.

A large PDF operation, image conversion or video-processing task might benefit considerably.

The right choice depends on the workload.

I find it useful to separate operations into three categories:

Lightweight tasks that can run directly on the main thread.

CPU-intensive tasks that should avoid blocking the interface.

Tasks that need additional capabilities or resources beyond ordinary browser processing.

While developing Pocket PDF, this distinction matters because different file formats have very different processing requirements.

The project brings PDF, image, audio, video and text utilities together in one browser-based interface.

No account, installation or file upload is required.

Project: https://www.pocketpdf.site

For anyone building browser applications, what's your preferred way of communicating progress and cancellation between a Web Worker and the main interface?

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.