title: Why does web worker performance sharply decline after 30 seconds?
body: <p>I'm trying to improve the performance of a script when executed in a web worker. It's designed to parse large text files in the browser without crashing. Everything works pretty well, but I notice a severe difference in performance for large files when using a web worker.</p>
  
  <p>So I conducted a simple experiment. I ran the script on the same input twice. The first run executed the script in the main thread of the page (no web workers). Naturally, this causes the page to freeze and become unresponsive. For the second run, I executed the script in a web worker.</p>
  
  <ul>
  <li><a href="https://github.com/mholt/jquery.parse/blob/version3/papaparse.js" rel="noreferrer">Script being executed</a></li>
  <li><a href="https://github.com/mholt/jquery.parse/tree/version3/player" rel="noreferrer">Test runner page</a></li>
  </ul>
  
  <p>For small files in this experiment (&lt; ~100 MB), the performance difference is negligible. However, on large files, parsing takes about 20x longer in the worker thread:</p>
  
  <p><img src="https://i.stack.imgur.com/OJUIv.png" alt="Performance of both scenarios on same graph"></p>
  
  <p>The blue line is expected. It should only take about 11 seconds to parse the file, and the performance is fairly steady:</p>
  
  <p><img src="https://i.stack.imgur.com/XjBvZ.png" alt="Performance of script without web worker"></p>
  
  <p>The red line is the performance inside the web worker. It is much more surprising:</p>
  
  <p><img src="https://i.stack.imgur.com/muSha.png" alt="Performance of script in web worker"></p>
  
  <p>The jagged line for the first 30 seconds is normal (the jag is caused by the slight delay in sending the results to the main thread after every chunk of the file is parsed). However, parsing slows down rather abruptly at 30 seconds. (Note that I'm only ever using a single web worker for the job; never more than one worker thread at a time.)</p>
  
  <p>I've confirmed that the delay is <em>not</em> in sending the results to the main thread with <code>postMessage()</code>. The slowdown is in <a href="https://github.com/mholt/jquery.parse/blob/version3/papaparse.js#L873" rel="noreferrer">the tight loop</a> of the parser, which is entirely synchronous. For reasons I can't explain, that loop is drastically slowed down and it gets slower with time after 30 seconds.</p>
  
  <p>But this only happens in a web worker. Running the same code in the main thread, as you've seen above, runs very smoothly and quickly.</p>
  
  <p>Why is this happening? What can I do to improve performance? (I don't expect anyone to fully understand all 1,200+ lines of code in that file. If you do, that's awesome, but I get the feeling this is more related to web workers than my code, since it runs fine in the main thread.)</p>
  
  <p><em>System: I'm running Chrome 35 on Mac OS 10.9.4 with 16 GB memory; quad-core 2.7 GHz Intel Core i7 with 256 KB L2 cache (per core) and L3 Cache of 6 MB. The file chunks are about 10 MB in size.</em></p>
  
  <p><strong>Update:</strong> Just tried it on Firefox 30 and it did <em>not</em> experience the same slowdown in a worker thread (but it was slower than Chrome when run in the main thread). However, trying the same experiment with an even larger file (about 1 GB) yielded significant slowdown after about 35-40 seconds (it seems).</p>