ncpu-web
multi-threaded library that browser run function worker
Installation
npm install ncpu-web
ncpu-web for the browser
environment,use ncpu for the node.js
environment.
require:Chrome60+,FireFox57+
Attention
Because it is multithreaded, context information cannot be received and parameter passing can only be achieved by cloning( The cloning will occur as described in the HTML structured clone algorithm, and an error will be thrown if the object cannot be cloned (e.g. because it contains functions)).
Quick Start
{ // ### run await NCPU // result: 3 await NCPU // result: 6 // ### pick const workerFibo = await NCPU; // slef time to run await +await // result: 102334155 //fibo(40) // ### getWorker // reuse a thread const ncpuWorker = NCPU; const multiplexingWorkerFibo = await NCPU; // reuse a thread const res = await Promiseall NCPU; // reuse a thread}
The above example spawns a Worker thread for each callback function when runing. In actual practice, use a pool of Workers instead for these kinds of tasks. Otherwise, the overhead of creating Workers would likely exceed their benefit.
License
ncpu-web is available under the MIT license. See the LICENSE file for details.