
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Common 'Thread' behavior for Browsers (using Web Workers) or Node (using Child Process).
Common "Thread" behavior for Browsers (using Web Workers) or Node (using Child Process).
This package attempts to provide thread-like behavior to JavaScript using Web Workers for the Browser or Child Process for Node.js. It has several advantages over some of its peers:
console option, but with a console.log selfie, the slave/child can do console.log back on the server.Because JavaScript lacks real thread support it uses thread like abstractions such as Web Workers or Child Process. These abstractions are actually spawning an entirely separate VM for execution. This can be expensive from both a time and a memory point of view. For this reason, Threadful (and any thread solution) should be used with thought and caution.
Threadful attempts to shave millis off the performance costs whereever possible. By having an Install mechanism, Threadful allows the developer to minimize the performance penalty assocated with dynamically executing functions in a Thread.
On average we have seen the following performance... Your own results may vary.
Require Threadful (on Node) ................... 4 ms.
Creating a Thread ............................. 8 ms.
Install a Function ............................ 74 ms.
Execute an Installed Function ................. 3 ms.
Execute a not previously installed Function ... 78 ms.
Threadify a Function .......................... 74 ms.
Execute a Threadified Function ................ 3 ms.
Given these numbers and other test, one should carefully weigh whether executing in a thread is necessary or not. Thread code that takes a long time to perform, generally does better in a thread than that which is quickly but often performed. Large calculations perform much better than small things, quality over quantity.
In our example code, we wrote a very simplistic prime number test and then let it run for 10 seconds. We compared running in the main thread to distributing the work across a large number of threads. Here are the results we saw...
Thread Number of Small Primes Large Primes
Model Threads Found in 10s Found in 10s
Used (incl Main) >0 >10000000
-------------- ----------- --------------- ---------------
Single Thread 1+0 23967 / --- 340 / ----
Multi Thread 1+1 11787 / 49% 314 / 92%
Multi Thread 1+2 16556 / 69% 635 / 187%
Multi Thread 1+4 20080 / 84% 1065 / 313%
Multi Thread 1+6 19238 / 80% 1230 / 362%
Multi Thread 1+8 17585 / 73% 1311 / 386%
Multi Thread 1+12 14683 / 61% 1305 / 384%
Multi Thread 1+16 12556 / 52% 1293 / 380%
Multi Thread 1+24 8585 / 36% 1263 / 371%
Multi Thread 1+48 3717 / 16% 1122 / 330%
(Test run on a 4 core intel chip with HyperThreading)
Our results indicate that when working with small primes, using threads was detrimental because the small primes are so quickly calculated individually. However, when we switched to large primes we saw immediate benefit from using thread because large prime calculations take more time individually. Our results also illustrate the diminishing return point of exceeding the number of "cores" available.
You can get Threadful from npm using the command
npm install Threadful
Once obtained you can use Threadful in your own code thus
Node.js - via require
// Require Threadful
var Threadful = require("Threadful");
Browser - Insert a script tag
<!-- Install Threadful -->
<script src="Threadful.js"></script>
Please Note: Installing Threadful in a browser create the global object Threadful.
Threadful uses callbacks to notify when things are complete. All callbacks have the form callback(error,result). If error is not null, some error occured during execution. Otherwise a valid result will be populated, if any was sent back.
ThreadfulThreadful.ThreadPoolYou can create a pool of threads just as easily using the new Threadful.ThreadPool(options) constructor. You just need to specify the number threads you wish in the pool. Threads can added or removed after the pool has been created with increaseThreadPoolSizeBy() and decreaseThreadPoolSizeBy().
Threadful.ThreadPool returns a new ThreadPool object. See below for details.
var threadpool = new Threadful.ThreadPool({
threads: 4,
timeout: 500
});
Threadful.ThreadYou can create a single Thread using the new Threadful.Thread() constructor.. Note that creating a single thread is really jsut a wrapper for creating a thread pool with 1 thread and a timeout of500ms.
Threadful.Thread returns a new ThreadPool object. See below for details.
// Create a new Thread
var thread = new Threadful.Thread();
Threadful.ThreadifyThreadify is a process that when given a function will create a Thread, install the function into the thread, and return a new function. When the new function is executed the actual execution is passed off to the original function in the thread. It essentially wraps a function in a Thread execution. It is provided purely for quick and easy access but does have some limitations...
Instead of using Threadful.Threadify consider creating a new Thread and using the Thread.threadify() method there.
Threadful.Threadify returns a new function, which serves as the means to execute the original function but on the thread created. The function will take any number of parameters, but the last parameter should be a callback function to be notified of the result of execution.
// Define some function
var f = function() { ... };
// Create a Threadify version of that function
var threaded = Threadful.Threadify(f);
// Execute the function f on a Thread.
threaded(1,2,3,callback); // execute function f on the thread, return result in the callback.
Threadful.ExecuteExecute is a one time thread execution for a passed in function. It create a single use Thread, installs the function into it, and then executes the function immediately. Once execution is complete the Thread is closed and no longer usable. Threadful.Execute has some very consquental limitations, so use with care.
Instead of using Threadful.Execute consider creating a new Thread, installing the function to the Thread, and using Thread.execute() to run it.
Execute returns nothing.
// Define some function
var f = function() { ... };
// Execute that function on a Thread.
Threadful.Execute(f,arg1,arg2,arg3,argEtc,callback);
Threadful.CloseAllCloses all previously created ThreadPool objects including those created by Threadful.Threadify and Threadful.Execute. Once a ThreadPool is closed it cannot be reused.
Threadful.CloseAll();
ThreadPoolOnce a Thread or ThreadPool is instantiated with new Thread() or new ThreadPool(), you can perform the following operations on either.
ThreadPool.install(name,f,callback)ThreadPool.uninstall(name,callback)ThreadPool.installed(name)ThreadPool.execute(name,arg,arg,arg,etc,callback)ThreadPool.distribute(name,iterable,resolver,callback)ThreadPool.map(name,iterable,resolver,callback)ThreadPool.filter(name,iterable,callback)ThreadPool.some(name,iterable,callback)ThreadPool.every(name,iterable,callback)ThreadPool.list()ThreadPool.status()ThreadPool.close()ThreadPool.threadify(f)ThreadPool.increaseThreadPoolSizeBy(int)ThreadPool.decreaseThreadPoolSizeBy(int)ThreadPool.getThreadPoolSize()ThreadPool.outstanding()ThreadPool.utilization()FAQs
Common 'Thread' behavior for Browsers (using Web Workers) or Node (using Child Process).
We found that threadful demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.