Question

Knowing that

  • Javascript is strictly single threaded and settimeout doesn't spawn new threads. Instead it follows event-loop model
  • Worker threads are new HTML5 features and its support is still not available in all HTML5 browsers

Which one I should be using for background data download purpose? If you have any experience or have any benchmark data available please share.

Not sure how it matters to question, but still for the sake of completeness I would like to mention that data is expected to be in XML format and multiple server-side services will be invoked to get the data. Is there a framework already available which caters to both hand-held device based browsers and desktop based browsers data downloading?

Was it helpful?

Solution 2

Worker thread are not supported on all major browsers yet, so I don't think it will be great idea to involve worker-thread into your design unless you only want to support latest browsers.

All the Ajax calls can be made in parallel, but I guess your problem will be how to know that all ajax call's response has come and data is inserted/updated into browser's database. One way to know that all AJAX responses have been dealt with would be to chain all the AJAX calls. But this will warrant you to hard-code the sequence of AJAX calls into your code-base. If that is not desirable than you would need a separate asynch flow to keep checking if all the AJAX calls have been responded to. You can achieve this by setting a flag for each ajax call and set that to true when the response has been used. Then you will keep checking the status of all the flag in an another flow (started using settimeout).

I am aware of any existing framework to take care of such complex activity, so you will have to write the code yourself for the same. Hope this would have helped you in giving some direction.

OTHER TIPS

Javascript is single threaded, but that doesn't mean data isn't downloaded in parallel. If you make an asynchronous AJAX call, you are downloading data in the background while the rest of your code is running.

Web workers are meant to do CPU-heavy work off the main thread. They won't help you get data any faster.

Here is a thread where they show how to run a bunch of parallel AJAX calls with JQuery Parallel asynchronous Ajax requests using jQuery

The only reason I can think of to use a worker thread would be to have it constantly talking with the server, but in that case websockets are a better solution http://www.html5rocks.com/en/tutorials/websockets/basics/

Web workers are only supported on IE10+ so you will still need a fall back solution if you want to support older versions of IE. http://msdn.microsoft.com/en-us/library/ie/hh673568(v=vs.85).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top