Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Workers handling AJAX calls - optimisation overkill?

I'm working with a code that handles all AJAX requests using Web Workers (when available). These workers do almost nothing more than XMLHttpRequest object handling (no extra computations). All requests created by workers are asynchronous (request.open("get",url,true)).

Recently, I got couple of issues regarding this code and I started to wonder if I should spend time fixing this or just dump the whole solution.

My research so far suggests that this code may be actually hurting performance. However, I wasn't able to find any credible source supporting this. My only two findings are:

  • 2 year old jQuery feature suggestion to use web workers for AJAX calls
  • this SO question that seems to ask about something a bit different (using synchronous requests in web workers vs AJAX calls)

Can someone point me to a reliable source discussing this issue? Or, are there any benchmarks that may dispel my doubts?

[EDIT] This question gets a little bit more interesting when WebWorker is also responsible for parsing the result (JSON.parse). Is asynchronous parsing improving performance?

like image 521
Konrad Dzwinel Avatar asked Sep 12 '13 15:09

Konrad Dzwinel


People also ask

Does AJAX improve performance?

AJAX can help you increase the performance of your website while reducing server load and increasing the overall user experience.

Are AJAX calls slow?

In sites that rely upon Ajax for functionality (or even pizzazz), performance becomes even more critical than the general JavaScript performance. Because Ajax requests take place behind the scenes, to the end user there is little discernible difference between an Ajax request being slow, and nothing happening at all.

Is there a way to limit the time an AJAX call will run?

As for limiting the number of AJAX calls per domain (either concurrent outstanding calls, or total calls made, or any other metric you might be interested in), the solution would be the venerable CS classic: add another layer of abstraction.


4 Answers

I have created a proper benchmark for that on jsperf. Depending on the browser, WebWorker approach is 85-95% slower than a raw ajax call.


Notes:

  • since network response time can be different for each request, I'm testing only new XMLHttpRequest() and JSON.parse(jsonString);. There are no real AJAX calls being made.
  • WebWorker setup and teardown operations are not being measured
  • note that I'm testing a single request, results for webworker approach may be better for multiple simultaneous requests
  • Calvin Metcalf explained to me that comparing sync and async on jsperf won't give accurate results and he created another benchmark that eliminates async overhead. Results still show that WebWorker approach is significantly slower.
  • From the Reddit discussion I learned that data passed between the main page and WebWorker are copied and have to be serialized in the process. Therefore, using WebWorker for parsing only doesn't make much sense, data will have to be serialized and deserialized anyway before you can use them on the main page.
like image 199
Konrad Dzwinel Avatar answered Oct 16 '22 08:10

Konrad Dzwinel


First thing to remember is that web workers rarely make things faster in the sense of taking less time, they make things faster in the sense that they off load computation to a background thread so that processing related to user interaction is not blocked. For instance when you take into account transferring the data, doing a huge calculation might take 8 seconds instead of 4. But if it was done on the main thread the entire page would be frozen for 4 seconds which is likely unacceptable.

With that in mind moving just the ajax calls off the main thread won't gain you anything as ajax calls are non blocking. But if you have to parse JSON or even better, extract a small subset out of a large request then a web worker can help you out.

A caveat i've heard but not confirmed is that workers use a different cache than the main page so that if the same resources are being loaded in the main thread and the worker it could cause a large duplication of effort.

like image 41
Calvin Avatar answered Oct 16 '22 08:10

Calvin


You are optimizing your code in the wrong place.

AJAX requests already run in a separate thread and return to the main event loop once they fulfil (and call the defined callback function).

Web workers are an interface to threads, meant for computationally expensive operations. Just like in classical desktop applications when you don't want to block the interface with computations that take a long time.

like image 7
Radu Potop Avatar answered Oct 16 '22 08:10

Radu Potop


Asynchronous IO is an important concept of Javascript.

First, your request is already asynchronous, the IO is non-blocking and during your request, you can run any another Javascript code. Executing the callback in a worker is much more interesting than the request.

Second, Javascript engines execute all code in the same thread, if you create new threads, you need to handle data communication with the worker message api (see Semaphore).

In conclusion, the asynchronous and single-threaded nature of JavaScript is powerful, use it as much as possible and create workers only if you really need it, for example in a long Javascript process.

like image 3
jsan Avatar answered Oct 16 '22 08:10

jsan