BetterThread
Betterthread allows you to easily write JavaScript that can be executed in parallel across threads and CPUs for high performance on systems with multiple cores or CPUs.
There are plenty of advanced options, but basic functionality is very easy to use:
const bt = require('betterthread');
const myWorker = new bt.ThreadedFunction((message, done)=>{
const foo = message + ' World!';
done(foo);
});
myWorker.on('data',(data)=>{
console.log(`I computed '${data}' in another thread!`)
myWorker.kill();
});
myWorker.send('Hello');
FAQ
Why do I need this?
Node.js doesn't provide a way to simply execute in another thread. Built-ins such as cluster
work great for sharing a HTTP server, but don't work well for general-purpse computing.
Do I need to use the experimental Worker feature in 10.5.0
?
No, this library does not require any experimental features and works on the current LTS version and old versions of Node; right now Node version 6.x.x
to 10.x.x
are supported.
How long does it take to spin up a new thread?
Starting a thread will take somewhere around half a second. You can test this by running node ./examples/spinupPerformance.js
.
What does the CPU usage of the main thread look like?
With the SHA example, the main thread's time is only 150mSec.
stonegray-vm2:betterthread stonegray$ ps -T -g 41943
PID TTY TIME CMD
41943 ttys005 0:00.15 node shaExample.js
41944 ttys005 0:06.26 /usr/local/bin/node ./betterthread/worker.js
41948 ttys006 0:00.01 ps -T -g 41943
What doesn't work in a ThreadedFunction?
Anything that can run in your main thread can run in a ThreadedFunction; there are currently two exceptions:
process.send()
and process.exit()
will not work as expected; they will apply to the worker not the parent. A patch for this is planned.- If you use the
cluster
library, (eg. running a multithreaded HTTP server) it will not work as expected at this time. A polyfill for cluster
is planned.
Can I nest threads within threads within threads?
Not right now. See above, process.send()
and cluster
need to be patched first.
What other neat things can I do with this that aren't in the examples?
- You can manually set the
uid
and gid
of a process to restrict the thread's permissions. - You can run your code in a V8 sandbox and only expose native APIs you specify.
What is the licence?
BetterThread is dual-licenced. For open-source noncommercial projects, BetterThread is available for anybody to freely use, modify, distrobute under the GPLv3.
Options
Defualt options:
{
verbose: false,
uid: undefined,
gid: undefined,
timeLimit: undefined,
vm: false
vmOpts: {
expose: ['require','console']
}
}