Is it possible to optimize two scripts using web workers? [closed]

て烟熏妆下的殇ゞ 提交于 2019-12-11 01:51:16

问题


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


回答1:


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.




回答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>



回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/14840984/is-it-possible-to-optimize-two-scripts-using-web-workers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!