Rinzler is a turboramjet parallel processing engine for the browser.
It speeds up your web application by allowing recurring, cpu-heavy functions to execute in parallel taking full advantage of the host system's available CPU cores.
Check out the full docs or read on for a quick start guide.
Install
npm i rinzler-engine
Both ES & UMD modules are bundled, as well as TypeScript types, so you should be all set.
Rinzler targets browsers with WebWorkers and Promises support (check the browserslistrc). Most modern evergreen browsers, including Edge, should be compatible.
Quick start
You first need to define functions for setting up the environment (optional) and processing job payloads.
In the following example, we will set up a Rinzler-accelerated app that decodes ArrayBuffer
s of utf8 text.
function init() {
self.params = {
encoding: 'utf-8'
}
}
function processJob(message) {
const buffer = message.encodedText
const text = new TextDecoder(self.params.encoding).decode(buffer)
return [text]
}
Next we will import Rinzler and start the engine by passing the two functions we defined above.
The following code is written for asynchronous contexts, but you can translate it to synchronous by using .then()
with a callback instead of await
.
import RinzlerEngine from 'rinzler-engine'
const engine = await new RinzlerEngine().configureAndStart(processJob, init)
Now we can actually run jobs! We'll use the runJob()
method, which returns a Promise
that will resolve when the job is completed.
Since we need to pass an ArrayBuffer
, we'll use the second argument as a Transferable[]
- much like in the native worker.postMessage()
API.
const encodedText = new TextEncoder().encode('hello Rinzler')
const decodedResult = await engine.runJob({ encodedText }, [encodedText])
console.log(decodedResult) // "hello Rinzler"
You can start as many jobs as you want, and take full advantage of ES6's asynchronous syntax (for example, Promise.all()
).
If you use TypeScript, you can pass return types with the runJob<T>(): Promise<T>
signature.
Under the hood, Rinzler will take care of launching Web Workers, balancing their load, and gracefully shutting them down when needed to reduce your app's memory footprint.