2

I'm running two libraries that are dependency aware. What I mean is the order of their execution does not matter. They will detect each other and run in any order.

script1.js

script2.js

Would I benefit by running these scripts in parallel (parallel processing) using web workers? (MDN Doc)

I mean would they run faster than if I just ran them serially?

Both scripts are already available on the client ( I don't have to download them ).

Sergiu Dumitriu
  • 11,455
  • 3
  • 39
  • 62
  • I don't think web workers are going to be helpful here. The documentation says that the communication is serialized into the JSON format, so unless you are doing any parallel processing, web workers aren't going to be of much use. – Blender Feb 12 '13 at 20:17
  • Web workers are meant to prevent *long-running* scripts from blocking, but you haven't mentioned whether your scripts will be long-running or not. Maybe you want the HTML5 [`async` attribute](https://developer.mozilla.org/en-US/docs/HTML/Element/script#attr-async) on your script elements? – apsillers Feb 12 '13 at 20:17
  • @apsillers - async is for the network request...i.e. so you don't wait on the download time...I already have them downloaded...I'm writing to .innerHTML to dynamcially execute, the script text which I already have available. –  Feb 12 '13 at 20:18
  • 1
    Wait, are you concerned about load time or execution time? You ask "`...would they load faster...?`", but in your comment just now you say you don't care about load time. – apsillers Feb 12 '13 at 20:21
  • 2
    Why do you care about loading at all if you don't need to download them? – Bergi Feb 12 '13 at 20:21
  • "Decting each other" will not be possible it they run in different WebWorkers. – Bergi Feb 12 '13 at 20:22
  • @Bergi - they would simply need to be managed by the main thread - `Messages can be sent to and from the main thread by using postMessage()` –  Feb 12 '13 at 21:13

4 Answers4

6

If one or both of the scripts is/are purely functional, meaning it doesn't need to access the DOM or any global Javascript objects, then you could benefit from using Web Workers.

If this is not the case, then Web Workers will do you no good. When JS is run through a web worker, it can only pass messages back to the main DOM thread and receive them.

Frances McMullin
  • 5,516
  • 2
  • 18
  • 19
2

No, javascript is executed on a single thread in the web browser. So you cannot run them in parallell unless they are not user interactive (does not interact with the DOM).

However, you can LOAD the script asynchronously to speedup initialization.

Simply use the async=true attribute on <script>. Like this:

<script async="async">....</script>
Mårten Wikström
  • 11,074
  • 5
  • 47
  • 87
1

As I understand them, web workers afford you an OS-level thread of execution. So performance improvements are likely dependent on the tradeoffs inherent in the client OS's threading implementation.

In any case, you should see an improvement in load times unless the OS has a terrible threading implementation.

CJ McAllister
  • 291
  • 3
  • 10
  • 20
  • No, webworkers do not necessarily get their own thread. It's open to the implementation to do that. Have a look at http://dev.opera.com/articles/view/web-workers-rise-up/ – Bergi Feb 12 '13 at 20:33
0

My bet, YES.

Its like asking

1 man takes 6 days to do a task. How much time will 2 men take?

:-)

Most ideal use-case for WebWorkers

Arindam
  • 998
  • 1
  • 8
  • 20