Working easy-web-worker examples running real Web Workers: a responsive page, progress and cancellation, a worker pool, transferable buffers and a worker with no file.Examples
Learn by doing
Less reading.
More understanding.
Five small applications running real Web Workers.
Run them, break them, and take the pattern back to yours.
Main thread1,020 msPage frozen
Worker61 framesPage kept moving
Responsiveness
01The same work, without the frozen page
Run one slow computation on the main thread and then inside a Worker, and measure what the page could still do in each case.
Open examplecountPrimes(8,000,000)62%
Lifecycle
02Watch the work, and stop it when you change your mind
A long prime search that reports how far it is, and stops inside the Worker the moment the call is canceled.
Open example3 segmentsmaxWorkers: 3
worker
worker-1
worker-2
Concurrency
03Three searches, one worker or three
Search three segments of numbers for the longest Collatz chain, with one Worker and then with three, and compare the time.
Open exampleCopy64 MBStill in the sender
Transfer0 MBMoved, not duplicated
Memory
04Send 64 MB to a Worker without copying it
Send the same buffer as a copy and as a transfer, and compare the time and what the sender is left holding.
Open examplecreateWorker(() => …)No file
A function is the worker
Types inferred
Runtime
05A function becomes the worker
Analyze text as you type inside a Worker created from a function, with its types inferred and no worker file to configure.
Open exampleLearn the model behind the examples.
A short path from a function to a typed worker.