StaticBug
StaticBug

Reputation: 577

Advantages of Web Workers and how they were achieved before?

I have read about Web Workers on http://www.whatwg.org/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?

Upvotes: 5

Views: 3161

Answers (2)

Gaurav
Gaurav

Reputation: 201

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 becomes unresponsive in case of running the complex 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 complex 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.

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382454

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 complex.

Upvotes: 7

Related Questions