Show HN: Automatic Workerization of JavaScript Code
codepen.io
codepen.io
This bit of code has several motivations:
1) it's a pain in the butt to have to put the code for a worker in a separate file sometimes, especially in small demos like on CodePen.
2) I don't want to have to maintain two different code paths if a particular browser doesn't support workers (though even IE does now, so this is likely a dying issue). I don't implement fallback checking in this demo, but it would be trivial to do so.
2.5) I also don't want to have to manually wire up message passing all the time. The messaging port that Worker provides is extremely primitive. It's technically only passing a single string!
3) It's just generally good to decouple long processes from the UI, and no, callbacks don't do that. Callbacks provide asynchronicity, not concurrency. This is concurrency, and pretty much at it's simplest.
4) in the process, I discovered a neat feature: you can run the expensive update on the worker and run a cheaper one that approximates the expected changes over time in the UI in cases where the full update runs much slower than 60fps and give a pretty good illusion of a much higher frame rate. It's like network lag compensation for video games, except within your web page.
Anyway, hope someone else funds this useful. Check out the editor view on CodePen for the code. It might even be possible to automatically wrap functions that return values in a Promise!