Pain-free web worker utilities.
Simply put, web workers are a HTML5 standard that allow you to run computationally intensive JS code in new threads, therefore keeping your user interface responsive.
simple-worker
makes using web workers super-simple.
Include the script first, of course.
To run a function once:
function intensiveFunction(n) {
// Long computations...
// instead of returning, use `postMessage`
postMessage(result);
}
SimpleWorker.run({
func: intensiveFunction,
args: [123456],
success: function(res) {
// do whatever you want
},
error: function(err) {
// do whatever you want
}
})
Create a worker for later use:
var worker = new SimpleWorker({
func: intensiveFunction,
success: function(res) {
// do whatever you want
},
error: function(err) {
// do whatever you want
}
})
// Run however many times your want
worker.run(123456)
worker.run(999999)
// Close the worker when you don't need it anymore
worker.close()
Excerpt from demo/
function fibo(n) {
function fiboHelper(m, a, b) {
if (m < n) {
var k = a + b;
a = b;
b = k;
return fiboHelper(++m, a, b);
} else {
return a;
}
}
postMessage(fiboHelper(0, 0, 1));
}
// Run once
SimpleWorker.run({
func: fibo,
args: [123],
success: function(res) {
console.log(res);
},
error: function(err) {
console.log(err);
}
});
// Create a worker for later use
var fiboWorker = new SimpleWorker({
func: fibo,
success: function(res) {
console.log(res);
},
error: function(err) {
console.log(err);
}
});
fiboWorker.run(123);
fiboWorker.run(456);
Simply open up demo/client/index.html
in your browser.
Or, if you prefer to run it via a server:
- CD into
demo/
npm install
node server.js
- Go to localhost:8000
The following restrictions apply to the function that you give to SimpleWorker
to run:
-
Closures won't work. Web workers have to be run in their own isolated environments; therefore you need to make sure your function does not make use of anything outside of its body.
-
Simicolons are not optional. You need to make sure you use simicolons to separate statements in your function; otherwise weird bugs might occur.
And of course, simple-worker
only works in browsers that support web workers. Check out the table at this page for the list of browsers.