I wish to run an function (in the background) using a worker. The data comes from a http request. I am using a mock calculation (e.data[0] * e.data[1] * xhrData.arr[3]
Yes this can be done by a service worker but :
The browser can terminate a service worker if it thinks the service worker has finished working. So be sure to wrap everything inside a promise and use event.waitUntil to make sure that the process isn't terminated before it has finished.
self.addEventListener("message", function(event) { event.waitUntil(test(event)); });
The service worker runs on a single thread, so if you do long synchronous operation you'd be slowing every request of your page. Service workers are not meant to do long calculation.
Yes shared workers can access the cache api. You can access it the same way you do from a service worker by using the global variable caches
caches.open(cacheName).then(function(cache) {
cache.match("...")
});