Menu

Moving PDF bytes to a worker without copying the whole buffer
📰

Moving PDF bytes to a worker without copying the whole buffer

DEV Community: javascript·KomoPDF·about 8 hours ago
#hMkYHdNl
#devtojavascript#devto#dev#status#file
Long article (813 words) — no AI summary yet.
Reading 0:00
15s threshold

A PDF editor moves a large file into a Web Worker to keep parsing off the main thread. The first message still pauses the interface: passing a buffer without a transfer list copies its bytes as part of structured cloning. For large files, that copy can be expensive.

Transferring an ArrayBuffer changes ownership instead. The worker receives the buffer, and the sender's buffer becomes detached. Its byteLength becomes zero. Plan for that ownership change before wiring it into a preview or export flow.

Transfer the file bytes

This example uses a module worker and reads a selected file into memory. Serve these files over HTTP from the same origin, rather than opening the HTML file directly.

<input id = "file" type = "file" accept = "application/pdf" > <output id = "status" ></output> <script type = "module" src = "./main.js" ></script>

Enter fullscreen mode

Exit fullscreen mode

// main.js const worker = new Worker ( './pdf-worker.js' , { type : 'module' } ) ; const status = document .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.