Hacker News new | ask | show | jobs
by possibleNoob 3780 days ago
Curious, how are Web workers helping you counter UI Thrashing?
1 comments

Web Workers essentially make your Javascript multi-threaded. What this means is you can run multiple processes concurrently. You do not have access to the DOM with Web Workers, but they are really handy for anything that is long running or memory intensive. The benefit of this is that you can allocate Web Workers to fetch data or run some long process and this will not disrupt the rendering thread within the DOM.

Initially, I was having the server load up large JSON objects inline with the DOM so that I could avoid doing multiple AJAX calls. This worked with 2 very large JSON Objects, but as soon as I hit 3, the page would have a noticeable jank trying to evaluate these JSON Objects and render the page.

I remembered this article from Hacker News a while back (http://www.pocketjavascript.com/blog/2015/11/23/introducing-...) and started using Web Workers based on the speed of the app (it's pretty fast, check it out pokedex.org). There was a HUGE difference with having that first initial page load with the AJAX calls in the background. The only thing I would recommend is that you should make your first page load not contingent upon any AJAX calls or any loops (ng-repeat in the case of Angular). This will allow you to load in the page without having to wait for any response from the server and usually, the page will load alongside the data retrieved from the Web Workers.