title: WebWorkers execution appears to be much slower than the main thread
body: <p>I have been working on optimizing some long running JAvaScript, and tried implementing WebWorkers.</p>
  
  <p>I have a collection of independent tasks to compute. In my initial test there are 80 tasks, and on the main thread the complete in 250ms. I thought that I could distribute the tasks across some web workers and get the time down to  maybe 50ms. </p>
  
  <p>My data is geometry data structures that nest multiple typed arrays. I have methods that extract all the data to JSON + an array of ArrayBuffer objects, and so I can pass the data as transfered to the WebWorker without duplicating the big arrays.</p>
  
  <ul>
  <li>I have tested the data transfer, and confirmed that it is working as expected. My Typed arrays are empty in the main thread after transferring to the WebWorker. </li>
  <li>I launch (for now) 4 web workers up front, so that when the work is needed to be done, the workers should be ready. </li>
  <li>As each worker finishes a task, I give it the next in the queue until the queue is empty. </li>
  <li>I track time in the web worker to see how much is being used doing the actual compute(e.g. ignoring data transfer overhead). </li>
  <li>I have an 8 core laptop which runs multi-threaded code daily. </li>
  </ul>
  
  <p>Here is my WebWorker script. </p>
  
  <pre><code>importScripts('../lib/MyLib.js');
  
  let timeComputing = 0;  
  this.onmessage = function(e) {
    switch (e.data.msg) {
      case 'compute':
          let mesh = ... unpack data;
          let start = performance.now();
          mesh.doexpensiveCompute();
          timeComputing += performance.now() - start;
          ... send data back to the main thread.
          break;
      case 'logTime':
          console.log("timeComputing:" + timeComputing);
    }
  }
  </code></pre>
  
  <p>When the worker logs the time being used, its usually around 130ms per worker, which means the total time is actually almost 500ms. The main thread does all the work in 250ms, so I'm going 100% slower using WebWorkers. For some reason, exactly the same code running in a WebWorker is much much slower than it does on the main thread. </p>
  
  <p>Some of the workloads I have soon might have hundreds of tasks, so I was hoping WebWorkers would keep my page responsive. (currently its not at all on big loads).</p>
  
  <p>Would anyone have any suggestions as to why I am seeing such poor results? Note: I have eliminated the cost of data transfer(which I believe to be minimal) and thread startup here. I am purely measuring the compute time in the worker, which is poor... Does anyone have experience running heavy compute tasks in a webworker?</p>
  
  <p>One idea, is that I my worker script also loads my main engine script. (MyLib.js in the example code), which is a Webpacked script, and quite big. I used this so that hopefully browser caching would mean it doesn't need to request it again. Maybe instead I should generate a minimal version of my engine just for the webworker context. </p>
  
  <p>Thanks for any tips...</p>