Skip to content

Commit d89c030

Browse files
committed
Try to spawn a worker without additional requests
If Blob and URL.createObjectURL we can save a web request and the additional evaluation of the given code.
1 parent 61bce33 commit d89c030

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

lib/thread.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,25 @@
9090
};
9191

9292
Thread.prototype._spawnWorker = function (cb) {
93-
var wrk = new Worker(this.options.path);
94-
wrk.postMessage(Thread.getWorkerSource(cb));
93+
var wrk;
94+
if (isNode) {
95+
wrk = new Worker(this.options.path);
96+
wrk.postMessage(Thread.getWorkerSource(cb));
97+
} else {
98+
try {
99+
var blob = new Blob([Thread.getWorkerSource(cb)], { type: 'text/javascript' });
100+
var url = URL.createObjectURL(blob);
101+
102+
wrk = new Worker(url);
103+
} catch (e) {
104+
if (this.options.ie10shim) { // blob/url unsupported, cross-origin error
105+
worker = new Worker(this.options.path);
106+
worker.postMessage(str);
107+
} else {
108+
throw e;
109+
}
110+
}
111+
}
95112

96113
return wrk;
97114
};

0 commit comments

Comments
 (0)