Making WebWorkers a safe environment

The current code (listed below) has been now in use in the Stackoverflow javascript chat room for a while and so far the toughest problem was Array(5000000000).join(“adasdadadasd”) instantly crashing some browser tabs for me when I was running the code executor bot. Monkeypatching Array.prototype.join seems to have fixed this and the maximum execution time of … Read more

Web Workers – How To Import Modules

ES2015 modules in Workers are available in Safari and in Chromium browsers. If other browsers / versions are your target, you still need to use importScripts(). When available, you create a module-worker like this: new Worker(“worker.js”, { type: “module” }); See: https://html.spec.whatwg.org/#module-worker-example These are the bug-reports for each browser: Firefox: In development (almost done) šŸ›‘ … Read more

Execute web worker from different origin

The best is probably to generate a simple worker-script dynamically, which will internally call importScripts(), which is not limited by this cross-origin restriction. To understand why you can’t use a cross-domain script as a Worker init-script, see this answer. Basically, the Worker context will have its own origin set to the one of that script. … Read more

Is there a faster way to yield to Javascript event loop than setTimeout(0)?

Yes, the message queue will have higher importance than timeouts one, and will thus fire at higher frequency. You can bind to that queue quite easily with the MessageChannel API: let i = 0; let j = 0; const channel = new MessageChannel(); channel.port1.onmessage = messageLoop; function messageLoop() { i++; // loop channel.port2.postMessage(“”); } function … Read more

Accessing localStorage from a webWorker

Web workers only have access to the following: XMLHttpRequest Application Cache Create other web workers navigator object location object setTimeout method clearTimeout method setInterval method clearInterval method Performance object (mark,measure,now methods: caniuse?) IndexedDB API (see: caniuse?) importScripts method JSON Worker The window or parent objects are not accessible from a Web worker therefore you can’t … Read more

Is there a way to create out of DOM elements in Web Worker?

Alright, I did some more research with the information @Bergi provided and found the following discussion on W3C mailing list: http://w3-org.9356.n7.nabble.com/Limited-DOM-in-Web-Workers-td44284.html And the excerpt that answers why there is no access to the XML parser or DOM parser in the Web Worker: You’re assuming that none of the DOM implementation code uses any sort of … Read more