Keeping the interface smooth when the file is huge
Why the page doesn't freeze while compressing a 500-page PDF, and how Web Workers and a little library called Comlink make that possible.
By the RatioAI team
If you've ever used a web app that locks up the moment you click a button, you know the feeling. The cursor still moves, but nothing responds. Scrolling stutters. Sometimes the browser asks if you want to kill the page.
That happens because JavaScript in a web page runs on a single main thread by default. The same thread that draws the UI and reacts to your clicks is also the one doing the heavy work. So when the work gets heavy, everything else waits.
Moving the heavy lifting off the main thread
Browsers have had a fix for this for a long time: Web Workers. A worker is a separate background thread with its own JavaScript environment. It can't touch the page directly, but it can do as much number-crunching as it likes without blocking the interface.
In RatioAI, the expensive stuff lives in workers: parsing documents, re-encoding images during compression, building merged files, rendering pages. The main thread mostly just handles the UI, which is exactly what it should be doing.
Making workers feel like normal functions
The raw worker API is message-based. You post a message, you listen for a reply, you match replies to requests yourself. It works, but it gets messy quickly when you have dozens of tools.
We use Comlink, a small library from the Chrome team, to hide that plumbing. With Comlink, calling into a worker looks like calling an async function. The tool code reads like ordinary code, and the threading details stay out of the way.
Little details that matter
A few smaller things also help the app feel quick:
- Transferring buffers to workers instead of copying them, so large files move between threads almost instantly.
- Reporting progress back from the worker, so you see a real progress indicator instead of a spinner.
- Letting you keep browsing pages and tweaking options while a job runs.
- Cancelling stale jobs when you change your mind, rather than finishing work nobody wants.
None of this is flashy, and that's the point. The best performance work is the kind you never notice. You drop a big file, pick a tool, and it just keeps feeling like a responsive app.