title: Number of Web Workers Limit
body: <p><strong>PROBLEM</strong></p>
  
  <p>I've discovered that there is a limit on the number of Web Workers that can be spawned by a browser.</p>
  
  <p><strong>Example</strong></p>
  
  <p>main HTML / JavaScript</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(document).ready(function(){
      var workers = new Array();
      var worker_index = 0;
      for (var i=0; i &lt; 25; i++) {
          workers[worker_index] = new Worker('test.worker.js');
          workers[worker_index].onmessage = function(event) {
              $("#debug").append('worker.onmessage i = ' + event.data + "&lt;br&gt;");
          };
          workers[worker_index].postMessage(i); // start the worker.      
  
          worker_index++;
      }   
  });
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="debug"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>test.worker.js</p>
  
  <pre><code>self.onmessage = function(event) {
      var i = event.data; 
  
      self.postMessage(i);
  };
  </code></pre>
  
  <p>This will generate only 20 output lines in the  container when using Firefox (version 14.0.1, Windows 7).  </p>
  
  <p><strong>QUESTION</strong></p>
  
  <p>Is there a way around this?  The only two ideas I can think of are:</p>
  
  <p>1) Daisy chaining the web workers, i.e., making each web worker spawn the next one</p>
  
  <p>Example:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(document).ready(function(){
      createWorker(0);
  });
  
  function createWorker(i) {
  
      var worker = new Worker('test.worker.js');
      worker.onmessage = function(event) {
          var index = event.data;
  
          $("#debug").append('worker.onmessage i = ' + index + "&lt;br&gt;");
  
          if ( index &lt; 25) {
              index++;
              createWorker(index);
          } 
      };
      worker.postMessage(i); // start the worker.
  }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="debug"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>2) Limit the number of web workers to a finite number and modify my code to work with that limit (i.e., share the work load across a finite number of web workers) - something like this: <a href="http://www.smartjava.org/content/html5-easily-parallelize-jobs-using-web-workers-and-threadpool" rel="noreferrer">http://www.smartjava.org/content/html5-easily-parallelize-jobs-using-web-workers-and-threadpool</a></p>
  
  <p>Unfortunately #1 doesn't seem to work (only a finite number of web workers will get spawned on a page load).  Are there any other solutions I should consider?</p>