title: Make web workers start sooner, without delay
body: <p>I'm trying to speed up a javascript website I'm building with a web worker. This is totally new for me, I'm learning.  </p>
  
  <p>I managed to have this web worker run and do what it should (i.e. load some CSV data, transform it and send it back).  </p>
  
  <p>But It seems to me that this web worker is starting his job a little bit too late (about 500 ms after I send him the postMessage)</p>
  
  <p>Is this normal ?<br>
  How come that the worker doesn't start sooner ?<br>
  What can I do ?</p>
  
  <p>Here is the worker:</p>
  
  <pre><code>self.postMessage('A. !!! Worker on');
  self.onmessage = function(event) {
      var n=event.data;
      self.postMessage('['+n+'] B. Worker started');
      // (...)
  }
  </code></pre>
  
  <p>Here is the handling of the worker:</p>
  
  <pre><code>window.timer = Date.now();
  window.timerTotal = window.timer;
  
  function loadData(n){
      console.log('A. !!! Begin loadData function (dt=' + (Date.now()-window.timer) + ') (T='+(Date.now()-window.timerTotal)+')'); window.timer = Date.now();
  
      var worker = new Worker('my_task.js');
      worker.onmessage = function(event) {
          if ( (typeof event.data)==='string'){
              console.log('Web Worker: ' + event.data + ' (dt=' + (Date.now()-window.timer) + ') (T='+(Date.now()-window.timerTotal)+')'); window.timer = Date.now();
          }
          else if ( (typeof event.data)==='object'){
              console.log('!!! Web Worker returned object: (dt=' + (Date.now()-window.timer) + ') (T='+(Date.now()-window.timerTotal)+')'); window.timer = Date.now();
              createGraphs2(event.data);
          }
      };
  
      if(n===100){
          d3.csv("data/Movies100-V2.0.csv", function(error, data_csv) {
              createGraphs(data_csv);
          });
      }
      else if(n==='10k'){
          worker.postMessage(n);
      console.log('postMessage send to web worker (dt=' + (Date.now()-window.timer) + ') (T='+(Date.now()-window.timerTotal)+')'); window.timer = Date.now();
      }
  }
  
  loadData('10k');
  
  // (...) lots of functions like createGraphs2(data)
  </code></pre>
  
  <p>And here is the log:</p>
  
  <blockquote>
    <p>Begin loadData function (dt=7) (T=7)<br>
    postMessage send to web worker (dt=1) (T=8)<br>
    Web Worker: A. !!! Worker on (dt=524) (T=591)<br>
    Web Worker: [10k] B. Worker started (dt=1) (T=593)  </p>
  </blockquote>
  
  <p>Thanks!</p>
  
  <h2>UPDATE:</h2>
  
  <p>When I send a postMessage to the worker at the same time as I launch a veryLongScript:</p>
  
  <pre><code>veryLongScript();
  worker.postMessage(n);
  </code></pre>
  
  <p>Most of the time, the worker responds after the veryLongScript.</p>
  
  <blockquote>
    <p>(veryLongScript)<br>
    Web Worker: A. !!! Worker on (dt=301) (T=12464)<br>
    Web Worker: [10k] B. Worker started (dt=1) (T=12465)<br>
    Web Worker: [10k] C. dependencies loaded (dt=600) (T=13065)  </p>
  </blockquote>
  
  <p>and sometimes the worker responds something before and after the veryLongScript, but it doesn't seem to work in parallel.</p>
  
  <blockquote>
    <p>Web Worker: A. !!! Worker on (dt=1235) (T=1416)<br>
    Web Worker: [10k] B. Worker started (dt=0) (T=1418)<br>
    (veryLongScript)<br>
    Web Worker: [10k] C. dependencies loaded (dt=619) (T=13168)  </p>
  </blockquote>