I know there is a limit on the number of concurrent http connections that can happen at a time in a browser...
Is this a total, overall browser limit? Are web-workers subject to this limit as well, or are they treated differently because they are different threads?
You can spawn as many workers as you wish. You can also pass data to the script being executed in the worker threads and also return value to the main thread upon completion.
A web worker is a JavaScript program running on a different thread, in parallel with main thread. The browser creates one thread per tab. The main thread can spawn an unlimited number of web workers, until the user's system resources are fully consumed.
TLDR: Workers (in Chrome and Firefox at least) appear to adhere to a global max number of connections per host name.
I used this code to create a number of workers...
/* jshint esversion: 6 */
(function() {
'use strict';
const iWorkerCount = 20;
for(let i = 0; i < iWorkerCount; i++) {
const oWorker = new Worker('/js/worker.js');
}
}());
... and then each worker made a remote request to an image service...
/* jshint esversion: 6 */
(function() {
'use strict';
const sUrl = 'https://loremflickr.com/320/240';
fetch(sUrl).then(sResponse => {
console.log(sResponse);
});
}());
There's an initial batch of requests that complete at the same time and then the remaining requests trickle in shortly once the number max requests dips
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With