Web Worker consumes massive amount of memory
<p>I am trying to improve performance in my app which compares content of two large canvases. </p>

<p>The problem is that during comparison main thread of the browser is blocked what results unresponsive UI. Execution time of comparison function takes approx 10ms and comparison happens every 250ms or 500ms. </p>

<p>To solve that problem I came up with idea of use another thread by creating a <code>web worker</code>. It works pretty nice right now, but I realized that it consumes insane amount of memory (up to 600MB in FF and Chrome - in Edge it's up to 70MB, it never reached 100MB in single thread version on all browsers). </p>

<p>I thought that I left some references and Garbage Collector can't release memory. However, after some time (whole day of stripping my app to the pieces and trying to <code>delete</code>, <code>null</code> or <code>undefined</code> variables/data) I created fiddle below in which I'm sending 1MB <code>ArrayBuffer</code> (even though I passed it by transferring) but without any processing and as you can see it consumes massive amount of memory too. </p>

<p>Any ideas how I can solve that problem (any alternative solutions or any possible worker improvements) and where the problem is?</p>

<pre><code>var sortFilterDataWorker = function () {
    onmessage = function image2compare(ev) {
      postMessage('hi');
    };
}.toString();

/* PREPARE WORKER AS STRING TO CREATE JS BLOB FILE */
sortFilterDataWorker = sortFilterDataWorker.slice(sortFilterDataWorker.indexOf('{') + 1, -1).trim();

var blob = new Blob([sortFilterDataWorker]) // create blob file with worker code
  , blobUrl = window.URL.createObjectURL(blob) // create pseudo url to blob file
  , compareWorker = new Worker(blobUrl) 
  ;

setInterval(function(){
  var oneMB = new ArrayBuffer(8388608);
  compareWorker.postMessage(oneMB, [oneMB]); // transpile ArrayBuffer
}, 250);
</code></pre>

<p><strong><a href="https://jsfiddle.net/98goyz2p/9/" rel="noreferrer">FIDDLE</a></strong></p>

<p><strong>EDIT:</strong></p>

<p>I found out that if I terminate worker every n repeats and create new one releases memory faster. But it's still not a solution just a curio.</p>