threading-js
Small wrapper for browser Web Workers that simplfies running tasks and allows running without having to serve a worker script to the client.
Use
Simple example showing how to use a Thread to interleave two arrays together using a SharedArrayBuffer. Using basic arrays increases the run time due to copying the data. ArrayBuffer ownership can be transferred using the transferList
parameter in run
and postMessage
.
The function being passed to the thread must be completely self-contained and only reference data in the passed 'context' object or loaded scripts. All data passed into the context object will be stringified to be copied.
With ES6 Imports
Example here
// Operation functionsconst interleave = { let i = 0 whiletrue if i >= alength || i >= blength break res2 * i + 0 = ai res2 * i + 1 = bi i++ return res} const threadFunc = { const arr1 = argsarr1 const arr2 = argsarr2 const res = argsres const data = return data} // Create threadconst thread = threadFunc interleave // Create dataconst ARRAY_LENGTH = 10000000const arr1 = ARRAY_LENGTH * 4const arr2 = ARRAY_LENGTH * 4const sharedres = ARRAY_LENGTH * 4 * 2forlet i = 0; i < ARRAY_LENGTH; i++ arr1i = Math arr2i = Math // Run the testsconsoleconsole consolethread // main thread run: 30.962158203125ms// starting// done// initial thread run: 111.95703125ms// starting// done// subsequent thread run: 35.179931640625ms
With UMD
Example here
Getting the Best Performance
Data Clone Pitfalls
When basic Javascript objects are transferred between the UI thread and a Web Worker (via run()
in this library), they are copied using the Structured Clone Algorithm, which introduces a significant overhead that can be so bad that it completely defeats the purpose of using a thread. Using shared or transferred buffers is preferable.
Transferable Objects and ArrayBuffers
Some objects can have their ownership transferred between the threads, removing the need for cloning the data and associated overhead. A buffer is transferred using the run()
function in this library and passing the object into the transferList
array. Once an object has been transferred it's no longer accessible from the original thread and must be explicitly transferred back using a call to postMessage()
. If an item is not in the transferList, then it is copied.
SharedArrayBuffers
SharedArrayBuffers are not copied, but don't need to be in the transferList
, either. These buffers can be read from multiple threads at once making them an ideal vessel for data processing and return objects. Synchronized writes, however, must be accounted for.
API
Thread
constructor(threadFunc, context, srcs)
The constructor takes a function to run, a dictionary of context data and functions for use in the thread function, and an array of remote source URLs to load libraries in from.
threadFunc
is the function to run in the worker. The value returned by this function will be passed back as the result of the run. postMessage
can be used in this, as well, to post intermediate results back to the main thread.
context
is a shallow dictionary of data or functions to be injected into the web worker scope.
srcs
is an array of script URLs to load from.
running
Whether or not the thread is running
ready
Whether or not the thread is ready
run(args, intermediateFunc, transferList)
Runs the thread function with the args value as an argument to the function.
intermediateFunc
is a callback to recieve intermediate postMessage results from the function. Use the intermediate postMessage function to transfer items as there's no way to return a list of items to transfer from thread function.
transferList
the equivelant of the postMessage
transfer list argument. Note that items in the transfer list are automatically retured once the run is completed.
Returns a promise.
cancel()
Cancels the current run and prepares for a new one.
dipose()
Disposes of the Thread data so it can't be used anymore.
ThreadPool
A thread pool for easily running many of the same type of task in parallel.
constructor(capacity, func, context, srcs, options)
Like the thread constructor, but takes a capacity as the first argument.
options.initializeImmediately = true: Creates all the threads immediately instead of lazily creating them so no intialization overhead is incurred.
ready
Whether the pool has inactive threads.
activeThreads
The number of currently inactive threads.
capacity
The total possible number of threads the pool can support.
run(...)
Get and use an available thread to run the requested task. Behaves like Thread.run
, but returns null
if there are not threads available in the pool.
dispose()
Dispose of all threads and data.
ThreadQueue
A helper class for creating a job queue and running through the tasks using as many threads to work as the capacity allows.
constructor(...)
Same as the ThreadPool
constructor.
run(...)
Same as Thread.run
.
dispose()
Same as ThreadPool.dispose