:horse: Distribute load on front-end via parallelism
5.5kb
JavaScript library for executing multiple parallel tasks on front-end
UMD
format i.e. library works everywhere (nodejs doesn't have the concept of workers but have a child-process concept which is not handled in this lib).~5.5 KB
gzipped.$ npm install super-workers
$ yarn add super-workers
$ bower install super-workers
Aim - To be able to run multiple tasks simultaneously without freezing the web-page
Below diagram will aid in understanding the flow better
Setting the workers configuration
Lets have the following worker configuration:
minWorkers = 1
maxWorkers = 3
. This number will restrict registering of workers more than this number. We can not set this number randomly since it depend on the number of cores in a machine. By default, the library will pick it for you using navigator.hardwareConcurrency
or it's polyfill. Setting it too high will not boost queue processing as it becomes constant after the threshold(totally depends upon the hardware configuration).Task Queue Process Management
medium
priority.Task Queue Process Management
reorders the TQ
based on tasks' prioriies.Web worker Execution
maxNumbers
is greater than the number of spawned workers.promise
.TQ
.Main Thread
var sp = new SuperWorkers.MainThread({
url: 'js/my-worker.js'
});
Constructor
accepts a configurable Object
with the following keys.
url
: The path of worker scriptmaxWorkers
: Maximum workers that can be spawned to execute parallel tasks. Depends on CPU cores(hardwareConcurrency) of a machineminWorkers
: Minimum workers that should be always available to execute parallel tasks (one worker should alive if tasks keep coming).killWorkersAfterJob
: Kills all the idle workers(no longer required as tasks are completed) except one worker.Config Keys | default | accepts |
---|---|---|
url | Undefined | String(url) |
maxWorkers | 3 | Number |
minWorkers | 1 | Number |
killWorkersAfterJob | true | Boolean |
Note: Url of the worker script is mandatory.
Worker Thread
// Include the library
importScripts('path/to/super-workers.min.js');
// It will expose `SuperWorkers`
var child = new SuperWorkers.WorkerThread();
Constructor
doesn't need any configuration but would accept a configurable Object
with different keys that would not be used by the library.
Refer above section on how to create an instance before hitting API methods.
exec
Executes a task
Parameter | Description | Accepts |
---|---|---|
method | Function that needs to be executed | Function(Offloading) / String(Onloading) |
params | List of arguments that the function accepts | Array |
config | Task config | Object |
function add (a, b, timeout) {
var p = SuperWorkers.Promise.defer();
setTimeout(function () {
return p.resolve(a + b);
}, timeout || 0);
return p.promise;
}
var taskConfig = {
name: 'Add',
priority: 'LOW',
};
sp.exec(add, [1, 2, 2000], taskConfig); // Offloading the method
Define add
mthod in worker script
importScripts('path/to/super-workers.min.js');
function add (a, b) {
return a + b;
}
In Main thread,
var taskConfig = {
name: 'Add',
priority: 'HIGH',
};
sp.exec('add', [1, 2, 2000], taskConfig); // Onloading the method
getAllWorkers
Returns the list of all the workers.
sp.getAllWorkers();
getActiveWorkers
Returns the list of all active
workers.
sp.getActiveWorkers();
getIdleWorkers
Returns the list of all idle
workers.
sp.getIdleWorkers();
getTerminatedWorkers
Returns the list of all the terminated
workers.
sp.getTerminatedWorkers();
terminateAllWorkers
Termiates all the workers irrespective of the status.
sp.terminateAllWorkers();
terminateWorker
Terminated a particular worker by passing the worker-id.
Parameter | Description |
---|---|
id | id of the worker to be terminated |
sp.terminateWorker('57cd47da-d98e-4a2d-814c-9b07cb51059c');
broadCastAll
Sends a same message
to all the active
workers (Message would be sent via postMessage
API)
Parameter | Description |
---|---|
msg | msg to be sent |
sp.broadCastAll('Hello my dear Child! A greeting from Parent.'});
broadCastTo
Sends a message
to a particular active
worker (Message would be sent via postMessage
API)
Parameter | Description |
---|---|
id | id of the worker to send a msg |
msg | msg to be sent |
sp.broadCastTo('57cd47da-d98e-4a2d-814c-9b07cb51059c', 'Hey! Can you run the script: worker.js? Thanks!');
sendMessage
Sends a message to Main thread.
Parameter | Description | Accepts |
---|---|---|
msg | msg to be sent | Object |
origin | origin | String |
child.sendMessage(obj, `'*'`);
exposeMethods
It works as a proxy. Methods which needs to be onloaded should be exposed before they can used in main thread.
Parameter | Description | Accepts |
---|---|---|
methods | list of all functions | Object |
child.exposeMethods({
add: function (a, b) {
return a + b;
}
});
get
Returns the task.
Parameter | Description |
---|---|
id | id of the task |
sp.taskQueue.get('34cd47da-d98e-4a2d-814c-9b07cb510578');
getAll
Returns the list of all all
tasks.
sp.taskQueue.getAll();
getActive
Returns the list of all active
tasks.
sp.taskQueue.getActive();
getCompleted
Returns the list of all completed
tasks.
sp.taskQueue.getCompleted();
getFailed
Returns the list of all the failed
tasks.
sp.taskQueue.getFailed();
tasks
and allTasks
Returns tasks which are in queue.
sp.taskQueue.tasks
Returns all the tasks i.e. pending, queued, active and completed/failed.
sp.taskQueue.allTasks
Tested in Chrome, Firefox and IE(versions >= 9).
src
compilation & bundling and dist
generation.ES6 source files
|
|
webpack
|
+--- babel, eslint
|
o--- tests and covergae
|
ready to use
library
in UMD format
npm run build
- produces production version(minified) of the library under the dist
foldernpm run dev
- produces development version(un minified) of the library and runs a watcher to detect file changes.npm run test
- well ... it runs the tests :)Note: If adding site to the list of sites using super-workers, please mention where to verify this in the PR description.
Please don't hesitate to buy me a cup of coffee :)
The MIT license (MIT)
Copyright (c) 2017 Varun Malhotra
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.