最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Advantages of Web Workers and how they were achieved before? - Stack Overflow

programmeradmin5浏览0评论

I have read about Web Workers on .html and I think I understand their purpose, but I am wondering if one of the main purposes of web workers, namely "allows long tasks to be executed without yielding to keep the page responsive." could be already achieved without web workers? Like Registering Callbaks also allow long tasks to be executed, and only interrupt when they are ready, wtihout blocking, isn't that the same?

I have read about Web Workers on http://www.whatwg/specs/web-apps/current-work/multipage/workers.html and I think I understand their purpose, but I am wondering if one of the main purposes of web workers, namely "allows long tasks to be executed without yielding to keep the page responsive." could be already achieved without web workers? Like Registering Callbaks also allow long tasks to be executed, and only interrupt when they are ready, wtihout blocking, isn't that the same?

Share Improve this question asked Jan 17, 2013 at 14:02 StaticBugStaticBug 5775 silver badges17 bronze badges 1
  • 1 Related: How to avoid blocking the browser while doing heavy work? You need to fragment your long-running job into chunks and run those chunks asynchronously. It's sometimes a bit of a hassle, but certainly possible. – apsillers Commented Jan 17, 2013 at 14:34
Add a ment  | 

2 Answers 2

Reset to default 7

Callbacks allow you to manage concurrency. That is handling tasks. Not always in an easy way.

Not only do webworkers allow you to do concurrency in an easier way, they also let you have parallelism, that is tasks really running in parallel : they don't necessarily block each other and they don't block the UI.

In order to have a long javascript based running task in your browser before web worker, you had to micro-manage it to cut it in small parts in order to allow the UI to keep responsive. And of course having more than one long running task was more plex.

We know web browsers increased a lot over the past few years and it is primarily because of lot of work done on its engines, ex- V8 (Google), Chakra (Microsoft). The JavaScript so far runs in a single thread. The problem with single-threaded architecture is that it blocks the code and UI bees unresponsive in case of running the plex script. There are various ways to solve this problem:

  1. Offload work to the server, but to make apps faster fat client is preferred
  2. Use asynchronous calls, but many plex ecosystem of async calls & promises could lead into callback hell
  3. Leverage multi-threading. Interesting!

Web Workers solve this issue by providing the capability of multi-threading in JavaScript.

发布评论

评论列表(0)

  1. 暂无评论