Skip to content

Commit 167c15b

Browse files
committed
Factors spawnMapWorker out to avoid creating functions.
1 parent 6728f2b commit 167c15b

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

lib/parallel.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,37 +106,38 @@
106106
return this;
107107
};
108108

109+
Parallel.prototype._spawnMapWorker = function (i, cb, done) {
110+
var that = this;
111+
var wrk = new Worker(that.options.path);
112+
wrk.postMessage(Parallel.getWorkerSource(cb));
113+
wrk.postMessage(that.data[i]);
114+
wrk.onmessage = function (msg) {
115+
wrk.terminate();
116+
that.data[i] = msg.data;
117+
done();
118+
};
119+
};
120+
109121
Parallel.prototype.map = function (cb) {
110122
if (!this.data.length) {
111123
return this.spawn(cb);
112124
}
113125

114-
function spawnWorker(i) {
115-
var wrk = new Worker(that.options.path);
116-
wrk.postMessage(Parallel.getWorkerSource(cb));
117-
wrk.postMessage(that.data[i]);
118-
wrk.onmessage = function (msg) {
119-
wrk.terminate();
120-
that.data[i] = msg.data;
121-
done();
122-
};
123-
}
124-
125126
var that = this;
126127
var startedOps = 0;
127128
var doneOps = 0;
128129
function done() {
129130
if (++doneOps === that.data.length) {
130131
newOp.resolve(null, that.data);
131132
} else if (startedOps < that.data.length) {
132-
spawnWorker(++startedOps);
133+
this._spawnMapWorker(++startedOps, cb, done);
133134
}
134135
}
135136

136137
var newOp = new Operation();
137138
this.operation.then(function () {
138139
for (; startedOps - doneOps < that.options.maxWorkers && startedOps < that.data.length; ++startedOps) {
139-
spawnWorker(startedOps);
140+
that._spawnMapWorker(startedOps, cb, done);
140141
}
141142
});
142143
this.operation = newOp;

0 commit comments

Comments
 (0)