Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

betterthread

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

betterthread

Easily write high-performance multithreaded JavaScript

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

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); // pass result back to main thread
});

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:

{
    // Enable console logging
    verbose: false,

    /* You can request that the child processes be spawned with a different user ID or group ID. You will recieve an EPERM if this fails. */
    uid: undefined,
    gid: undefined,

    /* You can set an execution time limit for the thread; after which it will be killed automatically; millisecond units. */
    timeLimit: undefined,

    /* To restrict what the process can do, you can run it within a V8 Virtual Machine context. By default, a relatively permissive VM is used, but this can be tweaked. */
    vm: false
    vmOpts: {
        /* Expose native APIs in the VM; by default, only require() and console are available. Note that this allows you to require builtins such as `fs` and `https` which may be unwanted. */
        expose: ['require','console']
    }
}

Keywords

FAQs

Package last updated on 09 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc