Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A pool of webworkers
// From:
// Web Worker Pool
// size is the max number of arguments
function WorkerPool(size) {
var workers = 0,
jobs = [];
// url: the url of the worker script
// msg: the initial message to pass to the worker
// cb : the callback to recieve messages from postMessage.
// return true from cb to dismiss the worker and advance the queue.
// ctx: the context for cb.apply
this.queueJob = function(url, msg, cb, ctx) {
var job = {
"url": url,
"msg": msg,
"cb" : cb,
"ctx": ctx
if (workers < size) nextJob();
function nextJob() {
if (jobs.length) {
(function() {
var job = jobs.shift(),
worker = new Worker(job.url);
worker.addEventListener('message', function(e) {
if (,, worker)) {
delete worker;
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.